I have zipped some data into as following:
list1 = [1,33,3]
list2 = [2,44,23]
list3 = [[3,4,5,6],[3,4,5,3],[4,5,3,1]]
list4 = [4,34,4]
data = [list(x) for x in zip(list1, list2, list3, list4)]
However, list3 is a list of lists. So the output ended up being
[[1,2,[3,4,5,6],4],
[33,44,[3,4,5,3],34],
[3,23,[4,5,3,1],4]]
I would like to unpack the list inside the lists like the following:
[[1,2,3,4,5,6,4],
[33,44,3,4,5,3,34],
[3,23,4,5,3,1,4]]
What is the best way to do that in python?
Cheers
>Solution :
You could use iteration_utilities.deepflatten in a list comprehension:
>>> from iteration_utilities import deepflatten
>>> xs1 = [1, 33, 3]
>>> xs2 = [2, 44, 23]
>>> xs3 = [[3, 4, 5, 6], [3, 4, 5, 3], [4, 5, 3, 1]]
>>> xs4 = [4, 34, 4]
>>> data = [list(xs) for xs in zip(xs1, xs2, xs3, xs4)]
>>> data
[[1, 2, [3, 4, 5, 6], 4], [33, 44, [3, 4, 5, 3], 34], [3, 23, [4, 5, 3, 1], 4]]
>>> new_data = [list(deepflatten(xs)) for xs in data]
>>> new_data
[[1, 2, 3, 4, 5, 6, 4], [33, 44, 3, 4, 5, 3, 34], [3, 23, 4, 5, 3, 1, 4]]