Add element to 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.

Add element to tuple python

Tomerikoo

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

Add element to 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')

Add element to tuple python

julienc

17.7k17 gold badges80 silver badges80 bronze badges

answered Jul 2, 2014 at 15:25

Add element to 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

Add element to 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

Add element to tuple python

britodfbrbritodfbr

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

Add element to 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 18 hours ago

Add element to tuple python

New contributor

Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1

How do you add an element to a tuple?

Use the + operator to add an element to a tuple Use the syntax tuple + new_element , where new_element is a single or multiple item tuple, to append new_element to the end of tuple .

How do you add an element to a tuple in Python?

To insert an element into a tuple in Python: Use the list() class to convert the tuple to a list. Use the list. insert() method to insert the element into the list. Use the tuple() class to convert the list to a tuple.

Can we add value to tuple in Python?

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

How do I add a tuple to a tuple?

Changing a Tuple But, if the element is itself a mutable data type like a list, its nested items can be changed. We can also assign a tuple to different values (reassignment). We can use + operator to combine two tuples. This is called concatenation.