Monday, 9 September 2013

How to get a list of Xth elements from a list of tuples?

How to get a list of Xth elements from a list of tuples?

I need a sequential list of the all the first elements and another
sequential list of all the 2nd elements in the list of tuples.
sset = [('foo',1),('bar',3),('zzz',9)]
x = ['foo','bar','zzz']
y = [1,3,9]
I am doing it this way:
x = [i for i,j in sset]
y = [j for i,j in sset]
Is there a simpler way?
I could use dict.keys() and dict.values() but is the order of the list
kept? Also if my tuples have >2 elements the dict trick doesn't work.
x,y = dict(sset).keys(), dict(sset).values()

No comments:

Post a Comment