How do you add items to a tuple in python?

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

16.6k15 gold badges37 silver badges54 bronze badges

asked May 24, 2013 at 8:04

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

a = ['2',]
b = 'z'
new = a + [b,]

answered May 24, 2013 at 8:05

Jon ClementsJon Clements

134k31 gold badges240 silver badges273 bronze badges

5

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 Aug 28, 2016 at 15:56

nitelynitely

2,06722 silver badges23 bronze badges

2

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

2452 silver badges5 bronze badges

answered May 24, 2013 at 8:22

kiriloffkiriloff

24.8k34 gold badges141 silver badges217 bronze badges

3

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.8k17 gold badges80 silver badges80 bronze badges

answered Jul 2, 2014 at 15:25

>>> x = [u'2',]
>>> x += u"random string"

Traceback [most recent call last]:
  File "", line 1, in 
    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']

answered May 24, 2013 at 8:05

jamylakjamylak

123k29 gold badges227 silver badges227 bronze badges

0

#1 form

a = ['x', 'y']
b = a + ['z',]
print[b]

#2 form

a = ['x', 'y']
b = a + tuple['b']
print[b]

answered Aug 18, 2017 at 16:27

britodfbrbritodfbr

1,56513 silver badges16 bronze badges

1

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]

answered Feb 7, 2020 at 5:20

If the comma bugs you, you can specify it's a tuple using tuple[].

ex_tuple = ['a', 'b']
ex_tuple += tuple['c']
print[ex_tuple]

answered Sep 15 at 2:41

2

How do you add data to a tuple in Python?

Add/insert items to tuple If you want to add new items at the beginning or the end to tuple , you can concatenate it with the + operator as described above, but if you want to insert a new item at any position, you need to convert a tuple to a list. Convert tuple to list with list[] . Insert an item with insert[] .

Can you add things to a tuple Python?

You can't add elements to a tuple because of their immutable property.

Chủ Đề