Home > OS >  list of tuples to single list
list of tuples to single list

Time:10-07

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.

CodePudding user response:

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']]
  • Related