How To Create A Tuple Of Tuples In Python?
I want to combine: A = (1,3,5) B = (2,4,6) into: C = ((1,2), (3,4), (5,6)) Is there a function that does this in python?
Solution 1:
Yes:
tuple(zip(A, B))
And this is all. The result will be as follows (both in Python 2.x and 3.x):
>>> tuple(zip(A, B))
((1, 2), (3, 4), (5, 6))
Solution 2:
You want to use zip
:
zip((1,3,5),(2,4,6))
This will technically return a list
on python2.x and a iterable object on python3.x. To get a tuple
of tuples, you would just enclose the whole thing in tuple(zip((1,3,5),(2,4,6)))
Post a Comment for "How To Create A Tuple Of Tuples In Python?"