Skip to content

Remove last character from list Python | Example code

  • by

The slice notation [:-1] is the right approach to remove the last character from the list python. You can also use the pop() and del function to delete the last value-form list.

Example remove the last element from a Python list

Simple example code.

Using Slicing

Slice operation returns a new list obtain a sublist containing all elements of the list except the last one.

list1 = [1, 2, 3, 4]
print(list1[:-1])

Output:

Remove last character from list python

Using list pop() function

It removes and returns the last element in the list if specify any index no passed in pop() method.

list1 = [1, 2, 3, 4]
print(list1.pop())

print(list1)

Output:

4
[1, 2, 3]

Using del statement

This function does not return the removed element.

list1 = [1, 2, 3, 4]
del list1[-1]

print(list1)

Output: [1, 2, 3]

Do comment if you have any doubts and suggestions on this Python List character topic.

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