How to destack nested tuples in Python?

PythonNestedTuples

Python Problem Overview


How to convert the following tuple:

from:

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

to:

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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

a = (1, 2)
b = (3, 4)

x = a + b

print(x)

Out:

(1, 2, 3, 4)

Solution 3 - Python

>>> tuple(j for i in (('aa', 'bb', 'cc'), 'dd') for j in (i if isinstance(i, tuple) else (i,)))
('aa', 'bb', 'cc', 'dd')

Solution 4 - Python

x = (('aa', 'bb', 'cc'), 'dd')
tuple(list(x[0]) + [x[1]])

Solution 5 - Python

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

You can simply do:

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

Result:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCoryView Question on Stackoverflow
Solution 1 - PythonVolatilityView Answer on Stackoverflow
Solution 2 - PythonThirumal AlaguView Answer on Stackoverflow
Solution 3 - PythonJohn La RooyView Answer on Stackoverflow
Solution 4 - PythonxvorsxView Answer on Stackoverflow
Solution 5 - PythonLunaView Answer on Stackoverflow