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.
10 Answers
You need to make the second element a 1-tuple, eg:
a = ('2',)
b = 'z'
new = a + (b,)
6 Comments
Learner
Why you need this comma
Joseph Young
@SIslam Without the comma, it will just be interpreted as brackets usually used to get around the order of precedence:
(a+b)*cILMostro_7
yeah, but you can do
new = a + b instead of new = a + (b,). AFAICT, works the same in python3 and python2.7.Jon Clements
@ILMostro_7 depends what b is though
artu-hnrq
Or shortly
a += ('z',), as mentioned in bellow answer |
Since Python 3.5 (PEP 448) you can do unpacking within a tuple, list set, and dict:
a = ('2',)
b = 'z'
new = (*a, b)
3 Comments
nocibambi
I am trying it on Python 3.7.10, and it works with
a = ('2'). That is without the additional comma.nitely
@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').Maxence1402
Though, from what I've tested, unpacking is much slower (x10 for small size, x2.5 for size 10**5) than constructing the tuple with addition.
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.
4 Comments
jamylak
This will be twice as slow as just adding two tuples
jamylak
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 +1kiriloff
two items including the first itemin list. but you are right, i should better add a longer=list example, see my edit
ShellDude
i kinda like this answer the best... while it is probably a bit more expensive, it looks very clean.
>>> 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')
Comments
#1 form
a = ('x', 'y')
b = a + ('z',)
print(b)
#2 form
a = ('x', 'y')
b = a + tuple('b')
print(b)
1 Comment
Pallav Jha
second option does not work.
TypeError: 'int' object is not iterableIf the comma bugs you, you can specify it's a tuple using tuple().
ex_tuple = ('a', 'b')
ex_tuple += tuple('c')
print(ex_tuple)
2 Comments
Thomas
Note: if 'c' is an int, you might as well add the comma (or use
str(c))Community
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.