Skip to content
Advertisement

list of tuples to single list

I have list of tuples in the following format,

[('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]

I need to convert them into following format,

[['ABC','32064','Woo'],
['ABC','32064','MISSI'],
['XYZ','32065','HAY']]

I have tried the following code

list1=[[('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]]
list2 = [item for sublist in list1 for item in sublist]
list2

but still producing the same result.

Advertisement

Answer

You could do it with a list comprehension:

data = [('ABC', ['32064', ['WOO', 'MISSI']]),
('XYZ', ['32065', ['HAY']])]

[[t[0],t[1][0],x] for t in data for x in t[1][1]]

Output:

[['ABC', '32064', 'WOO'], ['ABC', '32064', 'MISSI'], ['XYZ', '32065', 'HAY']]
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement