0

I would like to know how I can remove the last element of each list, which is stored in a list. I have this example:

a=[ [1, 2, 3, 4, ''], [ 4, 5, 6, 7, ''], [ 8, 8, 8, 8, '']]

And I want to remove the last element of each list:

a_removed=[[1, 2, 3, 4],[4, 5, 6, 7], [8, 8, 8, 8]]

I have tried to use a map function and filter function, but they only work in a single list, not in a list of list.

Any ideas on this?

Thank you

idillus
  • 221
  • 1
  • 2
  • 3

1 Answers1

4

You can use Explain Python's slice notation and a list comprehension:

>>> a = [[1, 2, 3, 4, ''], [4, 5, 6, 7, ''], [8, 8, 8, 8, '']]
>>> a_removed = [x[:-1] for x in a]
>>> a_removed
[[1, 2, 3, 4], [4, 5, 6, 7], [8, 8, 8, 8]]
>>>
Community
  • 1
  • 1