Python list without last element | Example code

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 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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.