Reputation: 272
The title is self explanatory, is there a one line expression to do so ?
Reproducible example:
Data = [(21,1,2000), (30,0,1991), (27,0,1994)]
age = [el[0] for el in Data]
sex = [el[1] for el in Data]
birth = [el[2] for el in Data]
print(age, sex, birth)
Upvotes: 1
Views: 317
Reputation: 1
# You can do this by combining zip() with map() :
Data = [(21,1,2000), (30,0,1991), (27,0,1994)]
result = list(map(list, zip(*Data)))
print(result)
# [[21, 30, 27], [1, 0, 0], [2000, 1991, 1994]]
Upvotes: 0
Reputation: 11238
A little modification to @Corralien solution, to get the result in list
not in tuple
, use map
, zip
together to get the result
Data = [(21,1,2000), (30,0,1991), (27,0,1994)]
age, sex, birth = map(list, zip(*Data))
print(age, sex, birth, sep ='\n')
output
[21, 30, 27] # age
[1, 0, 0] # sex
[2000, 1991, 1994] # birth
Upvotes: 4
Reputation: 120469
Use zip
:
age, sex, birth = zip(*Data) # tuple
# OR
age, sex, birth = map(list, zip(*Data)) # list
>>> age
[21, 30, 27]
>>> sex
[1, 0, 0]
>> birth
[2000, 1991, 1994]
Upvotes: 11