212

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 + new.id got error can only concatenate tuple (not "unicode") to tuple.

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Goran
  • 6,054
  • 10
  • 32
  • 54

7 Answers7

375

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

a = ('2',)
b = 'z'
new = a + (b,)
Jon Clements
  • 132,101
  • 31
  • 237
  • 267
87

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

a = ('2',)
b = 'z'
new = (*a, b)
nitely
  • 2,027
  • 22
  • 23
  • 1
    I am trying it on Python 3.7.10, and it works with `a = ('2')`. That is without the additional comma. – nocibambi May 22 '21 at 11:42
  • 1
    @nocibambi the comma makes it a tuple, without it it's just a string. Try `a = ('23')` and `new` becomes `('2', '3', 'z')`. If you add the comma then you get `('23', 'z')`. – nitely Aug 08 '21 at 05:42
39

From tuple to list to tuple :

a = ('2',)
b = 'b'

l = list(a)
l.append(b)

tuple(l)

Or with a longer list of items to append

a = ('2',)
items = ['o', 'k', 'd', 'o']

l = list(a)

for x in items:
    l.append(x)

print tuple(l)

gives you

>>> 
('2', 'o', 'k', 'd', 'o')

The point here is: List is a mutable sequence type. So you can change a given list by adding or removing elements. Tuple is an immutable sequence type. You can't change a tuple. So you have to create a new one.

modesto
  • 245
  • 2
  • 5
kiriloff
  • 24,401
  • 34
  • 141
  • 212
  • 6
    This will be twice as slow as just adding two tuples – jamylak May 24 '13 at 08:34
  • 3
    However if you note to OP to convert to `list` at the beginning, append items, and then at the very end convert to `tuple` then this is the best solution +1 – jamylak May 24 '13 at 09:04
  • two items including the first itemin list. but you are right, i should better add a longer=list example, see my edit – kiriloff May 24 '13 at 10:41
16

Tuple can only allow adding tuple to it. The best way to do it is:

mytuple =(u'2',)
mytuple +=(new.id,)

I tried the same scenario with the below data it all seems to be working fine.

>>> mytuple = (u'2',)
>>> mytuple += ('example text',)
>>> print mytuple
(u'2','example text')
julienc
  • 17,267
  • 17
  • 78
  • 79
12
>>> x = (u'2',)
>>> x += u"random string"

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    x += u"random string"
TypeError: can only concatenate tuple (not "unicode") to tuple
>>> x += (u"random string", )  # concatenate a one-tuple instead
>>> x
(u'2', u'random string')
jamylak
  • 120,885
  • 29
  • 225
  • 225
7

#1 form

a = ('x', 'y')
b = a + ('z',)
print(b)

#2 form

a = ('x', 'y')
b = a + tuple('b')
print(b)
britodfbr
  • 1,495
  • 12
  • 16
3

Bottom line, the easiest way to append to a tuple is to enclose the element being added with parentheses and a comma.

t = ('a', 4, 'string')
t = t + (5.0,)
print(t)

out: ('a', 4, 'string', 5.0)
alphahmed
  • 79
  • 4