Skip to content
Advertisement

How to remove the first and last item in a list?

I have the List

['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 '] 

How do I remove the first element, Q and 0002, the last element?

Advertisement

Answer

I’m not sure exactly what you want since it is unclear but this should help. You actually only have a single element in that list.

Assuming all your list items are strings with spaces as delimiters, here is how you can remove the first and last group of characters from each string in the list.

>>> L = ['Q 0006 005C 0078 0030 0030 0033 0034 ONE_OF 0002 ']
>>> [' '.join(el.split()[1:-1]) for el in L]
['0006 005C 0078 0030 0030 0033 0034 ONE_OF']
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement