Katsu
Katsu

Reputation: 111

Remove bracket in a list of tuples in tuples

I would like to remove the bracket of a tuples who belong to tuples within a list:

Here's an example:

List_1 = [(0, (1, 1)),
 (0, (1, 2)),
 (0, (1, 3))]

And the expected output should look like this:

list_2 = [(0, 1, 1),
 (0, 1, 2),
 (0, 1, 3)]

I tried this:

for element in List_n_d1d2:
    newlist = (element[0], element[1])

But ended up with the same output... Could you please help me, thank you !

Upvotes: 0

Views: 53

Answers (2)

Barmar
Barmar

Reputation: 780899

Use the * spread operator to expand the tuple into separate elements.

list_2 = [(a, *b) for a, b in list_1]

Upvotes: 3

John Coleman
John Coleman

Reputation: 51998

A simple comprehension would be:

[(a,) + b for a,b in List_1]

Upvotes: 3

Related Questions