Skip to content

Python list without last element | Example code

  • by

You can go through the list without the last element or remove the last element from the list in Python.

Python list without last element examples

Simple example code.

Go through the list without the last element in Python

Simply use slice notation to skip the last element from the list.

lst = [1, 2, 3, 4, 5]

for i in lst[:-1]:
    print(i, end=' ')

Output:

Python list without last element

Remove the last element from list python

Removing the last element using the slicing method.

record = [1, 2, 3, 4, 5]

record = record[:-1]

print(record)

Output: [1, 2, 3, 4]

But a better way is to delete the item directly, using the del keyword.

del record[-1]

Do comment if you have any doubts and suggestions on this list tutorial.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading