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


Tuples in Python are immutable, meaning that once they are created, their contents cannot be changed. However, there are situations when we want to change the existing tuple, in which case we must make a new tuple using only the changed elements from the original tuple.

Following is the example of the tuple −

s = (4,5,6) print(s) print(type(s))

Following is the output of the above code −

(4, 5, 6)

Tuple is immutable, although you can use the + operator to concatenate several tuples. The old object is still present at this point, and a new object is created.

Append elements in Tuple

Tuple is immutable, although you can use the + operator to concatenate several tuples. The old object is still present at this point, and a new object is created.

Example

Following is an example to append the tuple −

s=(2,5,8) s_append = s + (8, 16, 67) print(s_append) print(s)

Output

Following is an output of the above code −

(2, 5, 8, 8, 16, 67)
(2, 5, 8)

Note− Concatenation is only possible with tuples. It can't be concatenated to other kinds, such lists.

Example

Following is an example of concatenating a tuple with a list −

s=(2,5,8) s_append = (s + [8, 16, 67]) print(s_append) print(s)

Output

The following error came as an output of the above code −

Traceback (most recent call last):
  File "main.py", line 2, in 
    s_append = (s + [8, 16, 67])
TypeError: can only concatenate tuple (not "list") to tuple

Concatenating Tuple with one element

You can concatenate a tuple with one element if you want to add an item to it.

Example

Following is an example to concatenate a tuple with one element −

s=(2,5,8) s_append_one = s + (4,) print(s_append_one)

Output

Following is an output of the above code.

(2, 5, 8, 4)

Note − The end of a tuple with only one element must contain a comma as seen in the above example.

Adding/Inserting items in a Tuple

You can concatenate a tuple by adding new items to the beginning or end as previously mentioned; but, if you wish to insert a new item at any location, you must convert the tuple to a list.

Example

Following is an example of adding items in tuple −

s= (2,5,8) x = list(s) print(x) print(type(x)) x.insert(5, 90) print(x) s_insert = tuple(x) print(s_insert) print(type(s_insert))

Output

we get the following output of the above code.

[2, 5, 8]
⁢class 'list'>
[2, 5, 8, 90]
(2, 5, 8, 90)
⁢class 'tuple'>

Using append() method

A new element is added to the end of the list using the append() method.

Example

Following is an example to append an element using append() method −

t=(45,67,36,85,32) l = list(t) print(l) print(type(l)) l.append(787) print(l) t=tuple(l) print(t)

Output

Following is an output of the above code

[45, 67, 36, 85, 32]
⁢class 'list'>
[45, 67, 36, 85, 32, 787]
(45, 67, 36, 85, 32, 787)

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

Updated on 26-Aug-2022 15:33:51

  • Related Questions & Answers
  • How can I append a tuple into another tuple in Python?
  • Delete Tuple Elements in Python
  • Raise elements of tuple as power to another tuple in Python
  • How to get unique elements in nested tuple in Python
  • How to append elements to a Pandas series?
  • Modulo of tuple elements in Python
  • How to group Python tuple elements by their first element?
  • Python - Join tuple elements in a list
  • How to append a list to a Pandas DataFrame using append() in Python?
  • Count the elements till first tuple in Python
  • Python – Concatenate Rear elements in Tuple List
  • How to append objects in a list in Python?
  • Maximum and Minimum K elements in Tuple using Python
  • Python – Filter tuple with all same elements
  • C# Program to access tuple elements

How do you add one element to a tuple?

To create a tuple with only one item, you have add a comma after the item, otherwise Python will not recognize the variable as a tuple.

Which method is used in tuple for adding element?

Using append() method A new element is added to the end of the list using the append() method.