Python add item to the tuple


I have some object.ID-s which I try to store in the user session as tuple. When I add first one it works but tuple looks like (u'2',) but when I try to add new one using mytuple = mytuple + got error can only concatenate tuple (not "unicode") to tuple.

This question is tagged with python python-2.7 tuples

~ Asked on 2013-05-24 08:04:32

The Best Answer is


You need to make the second element a 1-tuple, eg:

a = ('2',)
b = 'z'
new = a + (b,)

~ Answered on 2013-05-24 08:05:52


Since Python 3.5 (PEP 448) you can do unpacking within a tuple, list set, and dict:

a = ('2',)
b = 'z'
new = (*a, b)

~ Answered on 2016-08-28 15:56:00

Most Viewed Questions: