How to destack nested tuples in Python?

Question:

How to convert the following tuple:

from:

(('aa', 'bb', 'cc'), 'dd')

to:

('aa', 'bb', 'cc', 'dd')
Asked By: Cory

||

Answers:

x = (('aa', 'bb', 'cc'), 'dd')
tuple(list(x[0]) + [x[1]])
Answered By: xvorsx
>>> tuple(j for i in (('aa', 'bb', 'cc'), 'dd') for j in (i if isinstance(i, tuple) else (i,)))
('aa', 'bb', 'cc', 'dd')
Answered By: John La Rooy
l = (('aa', 'bb', 'cc'), 'dd')
l = l[0] + (l[1],)

This will work for your situation, however John La Rooy’s solution is better for general cases.

Answered By: Volatility
a = (1, 2)
b = (3, 4)

x = a + b

print(x)

Out:

(1, 2, 3, 4)
Answered By: Thirumal Alagu
l = (('aa', 'bb', 'cc'), 'dd')

You can simply do:

(*l[0], l[1])

Result:

('aa', 'bb', 'cc', 'dd')
Answered By: Luna
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.