list merging in nested lists [on hold]

  • A+
Category:Languages

I have the following list:

[['a', [0, 1]], ['b', [2, 3]], ['c', [4, 5]]] 

I want to change it to:

[['a', 0, 1], ['b', 2, 3], ['c', 4, 5]]` 

How can I do it in Python 3 using list comprehension?

 


Since Python 3.5+, you can use unpacking in a list declaration. Use that inside a list-comprehension:

lst = [['a', [0, 1]], ['b', [2, 3]], ['c', [4, 5]]]  new_lst = [[x, *more] for x, more in lst]  print(new_lst) # [['a', 0, 1], ['b', 2, 3], ['c', 4, 5]] 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: