Can you add things to a tuple 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.

Can you add things to a tuple python?

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

Can you add things to a tuple python?

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')

Can you add things to a tuple python?

julienc

17.8k17 gold badges80 silver badges80 bronze badges

answered Jul 2, 2014 at 15:25

Can you add things to a tuple python?

>>> 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

Can you add things to a tuple python?

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

Can you add things to a tuple python?

britodfbrbritodfbr

1,57513 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

Can you add things to a tuple python?

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

Can you add things to a tuple python?

2

How do you add items to a tuple in Python?

Use the + operator to add an element to a tuple.
a_tuple = ("element1", "element2").
print(a_tuple) Output. ('element1', 'element2').
new_element = ("element3",).
a_tuple = a_tuple + new_element..
print(a_tuple) Output. ('element1', 'element2', 'element3').

Can you append values to a tuple?

Much like strings, tuple values can be altered or appended by simply concatenating a new value onto the existing one. It combines two different sets of tuples into one and doesn't actually change existing values, maintaining the immutability of the data type.

Is tuple editable in Python?

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

Can we update items in tuple?

Tuples are unchangeable, meaning that you cannot change, add, or remove items once the tuple is created.