0

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
just in
  • 23
  • 3
  • Does this answer your question? [Get the nth element from the inner list of a list of lists in Python](https://stackoverflow.com/questions/13188476/get-the-nth-element-from-the-inner-list-of-a-list-of-lists-in-python) – Gino Mempin Sep 08 '22 at 11:01

2 Answers2

2

you can use list comprehension

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

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

#=>[1, 2]
Ran A
  • 746
  • 3
  • 7
  • 19
1

Using map:

>>> from operator import itemgetter
>>> list(map(itemgetter(1), lst))
[1, 2]
Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31