Rearrange tuple of tuples in Python
Asked Answered
M

1

7

I have a tuple of tuples:

t = ((1, 'one'), (2, 'two'))

I need it in the following format:

((1, 2), ('one', 'two'))

How can I convert it? I can do something like:

digits     =  tuple ( digit for digit, word in t )
words      =  tuple ( word for digit, word in t )
rearranged =  tuple ( digits, words )

But that seems not elegant, I suppose there's a more straightforward solution.

Misleading answered 16/4, 2013 at 14:47 Comment(1)
This is called transposingLemon
R
14

Use the following:

tuple(zip(*t))
Riane answered 16/4, 2013 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.