I have a list which contains many lists and in those there 4 tuples.
JavaScript
x
3
1
my_list = [[(12, 1), (10, 3), (4, 0), (2, 0)],
2
[(110, 1), (34, 2), (12, 1), (55, 3)]]
3
I want them in two separate lists like:
JavaScript
1
3
1
my_list2 = [12,10,4,2,110,34,12,55]
2
my_list3 = [1,3,0,0,1,2,1,3]
3
my attempt was using the map
function for this.
JavaScript
1
2
1
my_list2 , my_list3 = map(list, zip(*my_list))
2
but this is giving me an error:
JavaScript
1
2
1
ValueError: too many values to unpack (expected 2)
2
Advertisement
Answer
Your approach is quite close, but you need to flatten first:
JavaScript
1
12
12
1
from itertools import chain
2
3
my_list = [[(12, 1), (10, 3), (4, 0), (2, 0)], [(110, 1), (34, 2), (12, 1), (55, 3)]]
4
5
my_list2 , my_list3 = map(list,zip(*chain.from_iterable(my_list)))
6
7
my_list2
8
# [12, 10, 4, 2, 110, 34, 12, 55]
9
10
my_list3
11
# [1, 3, 0, 0, 1, 2, 1, 3]
12