How to Append an Element to Tuple in Python

0
2001
Python append to tuple

After creating a tuple, you can’t add or remove any element. This is because tuples have no append() or extend() methods, but you can create a new tuple from an existing one.

What is a tuple?

Python tuple is an unordered, immutable data type used to store collections. Tuple data types are initialized by encapsulating values using parentheses ().

Python append to tuple

Appending a tuple in Python is impossible because it is an immutable object. But If you want to add an element to a tuple in Python, there’s no straightforward way.

What does immutable mean in Python?

An immutable means having a fixed value and being unable to change. Therefore, we are violating a principle of immutability by appending or removing an element from a tuple.

Appending to a tuple means creating a new object that adds value to an existing tuple.

How to append to a tuple?

To append to a tuple in Python:

  1. Convert the tuple to a list using the list() method.
  2. Append an element to the list.
  3. Convert the list to a tuple.

Step 1: Convert tuple to list

To convert a tuple to a list in Python, use the list() method.

tup = (11, 21, 19, 18, 29)

lst = list(tup)

print(lst)
print(type(lst))

Output

[11, 21, 19, 18, 29]
<class 'list'>

You can see that list() method converted a tuple to a list. The type() is a built-in function that allows you to check the data type of the parameter passed to it.

Step 2: Append an element to the list

To append an element to a list in Python, use the append() method.

tup = (11, 21, 19, 18, 29)

lst = list(tup)

lst.append(46)

print(lst)

Output

[11, 21, 19, 18, 29, 46]

And we appended the “46” element to the list.

Step 3: Convert a list to tuple.

To convert a list to a tuple, use the tuple() function.

tup = (11, 21, 19, 18, 29)

lst = list(tup)

lst.append(46)

tup = tuple(lst)

print(tup)

Output

(11, 21, 19, 18, 29, 46)

And we successfully append an element to a tuple using the conversions and append() method.

Using tuple concatenation

Tuple concatenation can be done using the + operator, which allows us to combine two tuples.

Let’s create a tuple (11, 21, 10), and we want to append the value 46 to it. Use the + operator to concatenate the value onto our tuple.

tuple_one = (11, 21, 19)
tuple_concat = tuple_one + (46, )
print(tuple_concat)

Output

(11, 21, 19, 46)

You can see that we appended a tuple to a tuple using the + operator. This is because we are appending a tuple to a tuple, not an element. If you are appending an element to a tuple, it will throw a TypeError.

Appending to a Tuple with Unpacking

To unpack a tuple in Python, use the asterisk(*). The unpacking operator, *, is used to access all the elements in a container object, such as a tuple.

To append to a tuple using unpacking, unpack all the values of the first tuple and then add the new value or values.

tuple_one = (11, 21, 19)
tuple_one = (*tuple_one, 46)
print(tuple_one)

Output

(11, 21, 19, 46)

We are unpacking a tuple and adding a 46 element to that tuple, and we get the appended value tuple in the result.

Conclusion

Appending an element to a tuple and appending a tuple to a tuple are completely different things.

The best way to append an element to a tuple, use the list conversion method

To append a tuple to a tuple, use the +(concatenation) operator.

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.