1

Take a 2d list. I want to make a new list with only the ith element from each list. What is the best way to do this?

I have:

 map(lambda x: x[i], l)

Here is an example

 >>> i = 0
 >>> l = [[1,10],[2,20],[3,30]]
 >>> map(lambda x: x[i], l)
 [1, 2, 3]
krumpelstiltskin
  • 486
  • 7
  • 17

1 Answers1

5

Use list comprehension:

i = 1
data = [[1,10],[2,20],[3,30]]
result = [d[i] for d in data]  # [10, 20, 30]

Also see this question on list comprehension vs. map.

Community
  • 1
  • 1
FMc
  • 41,963
  • 13
  • 79
  • 132