0

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.

sj95126
  • 6,520
  • 2
  • 15
  • 34
code_bug
  • 355
  • 1
  • 12

1 Answers1

1

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']]
John Coleman
  • 51,337
  • 7
  • 54
  • 119