Python tuples are immutable. Meaning you cannot change them after they are created. So, you cannot append any element directly to a tuple. However, you can concatenate elements by creating a new tuple that combines the original and new elements. It is a copy operation, not a modification in place.
Here are three main ways:
- Using concatenation (+) operator (Easy way)
- Convert to List and Back (Efficient for large tuples)
- Using += Operator (Rebinding)
Method 1: Using concatenation (+) operator
Using the concatenation (+) operator, combine the original tuple and the tuple to be added to create a new tuple. I highly recommend this approach if you are working with a small tuple.
Adding an element at the end
If you put your new element on the right side of the + operator, it will be added at the end.
main_tuple = (1, 2, 4, 5) new_element = 3 new_tuple = main_tuple + (new_element, ) print(new_tuple) # (1, 2, 4, 5, 3)
Adding an element at the beginning
If you put your new element on the left side of the + operator, it will append at the start of the tuple.
main_tuple = (1, 2, 4, 5) new_element = 3 prepend_new_tuple = (new_element, ) + main_tuple print(prepend_new_tuple) # (3, 1, 2, 4, 5)
Inserting in the middle
We will divide the tuple into two parts: before the desired insertion point (index of the tuple) and after the point. That will be two slices. Put the element to be “inserted” into its own tuple and then concatenate all three tuples and the final tuple has a newly added element at the center of the tuple.
main_tuple = (1, 2, 4, 5) new_element = 3 insert_index = 2 middle_new_tuple = main_tuple[:insert_index] + \ (new_element,) + main_tuple[insert_index:] print(middle_new_tuple) # (1, 2, 3, 4, 5)
Appending multiple elements
You can append multiple elements by combining the original with a new tuple that contains multiple elements.
main_tuple = (1, 2, 4, 5) new_tuple = (3, 6) multi_tuple = main_tuple + new_tuple print(multi_tuple) # (1, 2, 4, 5, 3, 6)
Pros
- It provides simple syntax that directly combines tuples in a readable way.
- The original tuple is intact, as the + operator will return a new tuple—a kind of pure programming practice.
- You can append wherever you like, as discussed in different use cases.
Cons
- It becomes inefficient if our tuple is large because it creates a new one every time.
- It requires explicit index management for middle insertions.
Method 2: Convert tuple to list and back
You cannot modify a tuple, but you can modify a list. Thus, we can convert a tuple into a list, add the elements, and then convert it back to the tuple. This is helpful when dealing with complicated modifications.
For inserting multiple elements into a list, you can use a list.extend() method.
main_tuple = (1, 2, 4, 5) main_list = list(main_tuple) elements_to_append = [11, 12, 13, 14] main_list.extend(elements_to_append) main_new_tuple = tuple(main_list) print(main_new_tuple) # (1, 2, 4, 5, 11, 12, 13, 14)
Pros
- It provides flexibility because we can perform any operation we want after converting to a list, such as addition, subtraction, and division.
- It gives the best performance when you are performing repeated addition. Efficient for bulk changes.
Cons
- It will add computation cost since you must convert it to a list.
- It requires explicit conversion steps.
Method 3: Using the += operator
Using the += operator on the original tuple will add new elements each time, but it will create a new variable with each iteration. If you keep the same variable name for each iteration, you might think that it does not return a new tuple, but it does. We are just giving each tuple the same name.
It is a Shorthand for tuple = tuple + new_element.
main_tuple = (1, 2, 4, 5) main_tuple += (3,) print(main_tuple) # (1, 2, 4, 5, 3)
Pros
- A quick way to add elements at the end.
- It provides concise syntax.
Cons
- If you keep the same naming for each tuple, it might create confusion for you. Behind the scenes, on each operation, it is creating a new tuple.
- For large tuples, it is not suitable.
Select any method that suits you best.