list merging in nested lists

anotherone

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?

Olivier Melançon

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]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related