22-Nov-2022
.
Admin
Hi Dev,
In this short tutorial we will cover an how to print all elements except last in python. I explained simply step by step python print all elements in list except last. This tutorial will give you simple example of python all elements except last. We will use python all items in list except last.
There are many ways to get all elements except the last element in list python. i will give you two examples using for loop with list[:-1] to except last element from list. so let's see the below examples.
so let's see following examples with output:
Example 1:
main.py
myList = [1, 2, 3, 4, 5]
# Get All Value Except Last One
myList = myList[:-1]
print(myList)
Output:
[1, 2, 3, 4]
Example 2:
main.py
myList = ["One", "Two", "Three", "Four", "Five"]
# Get All Value Except Last One
for listElem in myList[:-1]:
print(listElem)
Output:
One
Two
Three
Four
#Python