Home > Enterprise >  How to get every n-th element from a multi-dimensional Python list?
How to get every n-th element from a multi-dimensional Python list?

Time:09-09

From

lst = [ ['a', 1], ['b', 2] ]

How can I get a new list [1, 2] from it?

lst[0:2][1] works iteratively, so it doesn't work.

Is that possible without any loops?

CodePudding user response:

you can use list comprehension

lst = [ ['a', 1], ['b', 2] ]
n=1

result=[i[n] for i in lst ]

#=>[1, 2]

CodePudding user response:

Using map:

>>> from operator import itemgetter
>>> list(map(itemgetter(1), lst))
[1, 2]
  • Related