- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to add elements to a Tuple
In Python, tuples are immutable, meaning that once they are created, their elements cannot be modified. However, there may be times when you need to add elements to a tuple.
In this article, we will be discussing how to add elements to a tuple in Python. We will go over the syntax for adding elements to a tuple and will provide examples of how to do so.
Python tuples are very similar to Python lists, in that you can perform all the various operations that can be performed on lists. The only difference is that tuples cannot be modified after creation, i.e., their immutability as opposed to lists which are inherently mutable. We can define a tuple in python using the round brackets enclosing the data that we wish to store −
var = (1, ‘a’, 3.7)
But, as we just saw that tuples are immutable. So, how are we going to append or add elements into them? We know that the act of adding something usually means changing the item, but tuples cannot be modified after creation hence we cannot add elements to the same tuple.
If we try to add an element into the tuple using the append method or try to change the existing values in it, we will get a TypeError saying that “tuple does not support item assignment”.
The only option we have is to create an entirely new tuple with the data that wish for it to have. This way we can maintain the immutability while achieving our goal of adding elements into it.
We can various ways through which we can insert / add elements in a tuple.
Using + operator for concatenation
Using type conversion
Using the * operator
Using + operator
As you know, Python is a user-friendly language and many of methods make it easy for users to interact and solve complex problems without having to understand the algorithm or theory behind it.
Using the + operator for adding elements to a tuple is kind of the same. Whenever we use the + operator between tuples it results in the creation of a new tuple with values of both the tuples. This is true even if we are using the + operator between same variables.
Step 1 - Create a tuple with some values
Step 2 – Use the + operator to add more values / elements to it
The thing to note is that the type of objects on both sides of the operator must be the same, meaning that the other object must be a tuple if we wish to add elements. Otherwise, we will get an error.
Example
In the following example we are using the "+" operator on the same variable –
A = (1, 2, 3) print("The elements of tuple before adding are :", A) A += (4,) print("Tuple after addition of new elements", A)
Output
Tuple before addition of new elements(1, 2 , 3) Tuple after addition of new elements(1, 2, 3, 4)
Example
In the following example we are using the "+" operator on the different variable.
A = (1, 2, 3) B = (4,) print("The elements of tuple before adding are :", A) A += B print("Tuple after addition of new elements", A)
Output
Tuple before addition of new elements(1, 2 , 3) Tuple after addition of new elements(1, 2, 3, 4)
Using type conversion to list
In this method, we will be looking into using type conversion to add elements in the tuple. We know that we can use the list method to convert the tuples to lists and then we can make use of append method of list to add however many items we like, when we will be done with adding elements, we then convert the list to tuple using type conversion.
This is a roundabout way of adding elements, but it saves us the effort to check for type mismatch between + operators.
Example
A = (1, 2, 3) print("Tuple before addition of new elements", A) A = list(A) A.append(4) A.append(5) A.append(6) A = tuple(A) print("Tuple after addition of new elements", A)
Output
Tuple before addition of new elements(1, 2 , 3) Tuple after addition of new elements(1, 2, 3, 4, 5, 6)
Using the " * "operator
This is an advanced method as it is based on the application of unpacking elements of a tuple and using that to insert elements in the newly created tuple. The * operator is used for multiplying or repetition of elements, but based on its position it can behave differently. Whenever we use the * operator in front of a tuple it unpacks all the elements of the tuple.
Algorithm
Step 1 − Create a tuple
Step 2 − Create a new tuple with the same name and assign it the tuple object with adding the elements we wish to add
Example
A = (1, 2, 3) print("Tuple before addition of new elements", A) A = (*A, 4, 5, 6) print("Tuple after addition of new elements", A)
Output
Tuple before addition of new elements(1, 2, 3) Tuple after addition of new elements(1, 2, 3, 4, 5, 6)
Conclusion
In this article, we saw three different ways to add elements in a tuple. We learnt the pythonic way which is simple and easy to use. We saw, how can we make use of type conversion and list method, followed by append to add elements. At last we dived into the advanced concept of unpacking and its use to add elements.