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

From Dev

Nested list comprehension / merging nested lists

From Dev

Merging a list of lists of lists

From Dev

Merging a list of lists of lists

From Dev

Merging a list with a list of lists

From Dev

merging 2 nested lists in prolog

From Dev

Merging a list of strings and a list of lists

From Dev

Merging a list of lists into a single list with minimal comparisons

From Dev

Cracking this list of nested lists?

From Dev

Merging two sorted lists into a larger sorted list

From Dev

Python merging list of lists with varying length

From Dev

Merging to lists of different length to for list of dictionaries

From Dev

Find empty lists in nested list of lists

From Dev

List Comprehension of Lists Nested in Dictionaries

From Dev

Split a nested list in to separate lists

From Dev

list comprehension creating nested lists

From Dev

Split a nested list in to separate lists

From Dev

Nested arithmetic operation on a list of lists

From Dev

Merging lists of lists

From Dev

Nested List of Lists to Single List of tuples

From Dev

Merging 2 lists in Python, giving one list precedence

From Dev

merging two sorted linked lists into one linked list in python

From Dev

Merging ordered lists in F#, Expecting a 'a but given a 'a -> a' list

From Dev

How to convert a two nested list of lists into a nested list of tuples in Python?

From Dev

Merging(2 lists) X Coordinate List and Y Coordinate List to Get(1 list) XY Coordinate List

From Dev

How to find the number of nested lists in a list?

From Dev

Jackson list deserialization. nested Lists

From Dev

Create nested list from two lists

From Dev

Python Nested List Comprehensions on Multiple Lists

From Dev

inline lists with block nested list's for a menu

Related Related

HotTag

Archive