How to delete the very last character from every string in a list of strings
test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]
New test will contain the result ["8001","8003","8005"]
.
[x[:-1] for x in test]
creates a new list (using list comprehension) by looping over each item in test
and putting a modified version into newtest
. The x[:-1]
means to take everything in the string value x up to but not including the last element.
You are not so far off. Using the slice notation [:-1] is the right approach. Just combine it with a list comprehension:
>>> test = ['80010','80030','80050']
>>> [x[:-1] for x in test]
['8001', '8003', '8005']
somestring[:-1]
gives you everything from the character at position 0 (inclusive) to the last character (exclusive).