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.
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])
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)
[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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.