How to append elements in Python tuple?

ProgrammingPythonServer Side Programming

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

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 <module>
    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) # Python conversion for list and tuple to one another x = list(s) print(x) print(type(x)) # Add items by using insert () x.insert(5, 90) print(x) # Use tuple to convert a list to a tuple (). 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 −

# converting tuple to list t=(45,67,36,85,32) l = list(t) print(l) print(type(l)) # appending the element in a list l.append(787) print(l) # Converting the list to tuple using tuple() 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)
raja
Updated on 26-Aug-2022 15:33:51

Advertisements