Remove last character from list Python | Example code

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.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.