Concatenation of nested tuples

Given two variables

A = (2, 3)
B = (1, 4), (5, 8)

      

What is the simplest way to combine these two into a result variable C

so that:

C = ((2, 3), (1, 4), (5, 8))

      

Note that just calling:

C = A + B 

      

leads to:

C = (2, 3, (1, 4), (5, 8))

      

which is not the desired result.

Also, note that tuples are preferred over lists, so A

, B

and C

can be used elsewhere as dictionary keys.

+3


source to share


4 answers


I would say that you probably meant that the tuple A

is a nested tuple too:



>>> A = ((2, 3),)
>>> A + ((1,4), (5,8))
((2, 3), (1, 4), (5, 8))

      

+8


source


it

(A,) + B

      



Note that it is B

already a nested tuple and is (A,)

building one from a flat set A

.

+6


source


If you want more control, use lists. If it A

is a nested tuple, it will suffice to do this:

>>> A = (2, 3),
>>> B = (1, 4), (5, 8)
>>> A + B
((2, 3), (1, 4), (5, 8))

      

+1


source


Below code might help

>>> A = (2, 3)
>>> B = (4, 5)
>>> C = (A, B)
>>> C
((2, 3), (4, 5))

      

0


source







All Articles