I have list of tuples in the following format,
JavaScript
x
3
1
[('ABC', ['32064', ['WOO', 'MISSI']]),
2
('XYZ', ['32065', ['HAY']])]
3
I need to convert them into following format,
JavaScript
1
4
1
[['ABC','32064','Woo'],
2
['ABC','32064','MISSI'],
3
['XYZ','32065','HAY']]
4
I have tried the following code
JavaScript
1
5
1
list1=[[('ABC', ['32064', ['WOO', 'MISSI']]),
2
('XYZ', ['32065', ['HAY']])]]
3
list2 = [item for sublist in list1 for item in sublist]
4
list2
5
but still producing the same result.
Advertisement
Answer
You could do it with a list comprehension:
JavaScript
1
5
1
data = [('ABC', ['32064', ['WOO', 'MISSI']]),
2
('XYZ', ['32065', ['HAY']])]
3
4
[[t[0],t[1][0],x] for t in data for x in t[1][1]]
5
Output:
JavaScript
1
2
1
[['ABC', '32064', 'WOO'], ['ABC', '32064', 'MISSI'], ['XYZ', '32065', 'HAY']]
2