Properly slicing a list of lists

Question:

I have an input stream:

data = [[1,234],[2,432],[3,443]]

How to get the second element of every list? I can get the second value of a single entry by data[0][1], or every list in a range with both elements using data[0:2], but how to get just the second element from every list? Instead of looping, is there a better solution?

Asked By: PearsonArtPhoto

||

Answers:

Use a list comprehension:

[lst[1] for lst in data]
Answered By: Martijn Pieters

or use operator and map:

from operator import itemgetter
map(itemgetter(1), data)
Answered By: Artsiom Rudzenka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.