Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a tuple to a tuple in Python

Tags:

python

tuples

I have a tuple:

a = (1,2,3)

and I need to add a tuple at the end

b = (4,5)

The result should be:

(1,2,3,(4,5))

Even if I wrap b in extra parents: a + (b), I get (1,2,3,4,5) which is not what I wanted.

like image 929
Matej Avatar asked Dec 07 '22 03:12

Matej


1 Answers

When you do a + b you are simply concatenating both the tuples. Here, you want the entire tuple to be a part of another tuple. So, we wrap that inside another tuple.

a, b = (1, 2, 3), (4,5)
print a + (b,)  # (1, 2, 3, (4, 5))
like image 64
thefourtheye Avatar answered Dec 29 '22 04:12

thefourtheye