[Tutor] Convert tuple within tuple into single tuple

Peter Otten __peter__ at web.de
Wed Jan 11 16:02:36 EST 2017


ramakrishna reddy wrote:

> Hi All,
> 
> Is there any way to convert x = (1, 2, 3, (4, 5)) to x = (1, 2, 3, 4, 5)
> in python 2.7

Is the structure of x always the same? 

Then you can build a new tuple from

>>> x[:-1]  # all items but the last
(1, 2, 3)

and

>>> x[-1]  # the last item
(4, 5)

by concatenating them:

>>> x[:-1] + x[-1]
(1, 2, 3, 4, 5)




More information about the Tutor mailing list