I want to slice every string in a list in Python.
This is my current list:
['One', 'Two', 'Three', 'Four', 'Five']
This is the list I want for result:
['O', 'T', 'Thr', 'Fo', 'Fi']
I want to slice away the two last characters from every single string in my list.
How can I do this?
You can do:
Use a list comprehension to create a new list with the result of an expression applied to each element in the inputlist; here the
[:-2]
slices of the last two characters, returning the remainder:Demo: