Many times, while working with records, we can have a problem in which we need to add two records and store them together. This requires concatenation. As tuples are immutable, this task becomes little complex. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using + operator
This is the most Pythonic and recommended method to perform this particular task. In this, we add two tuples and return the concatenated tuple. No previous tuple is changed in this process.
# Python3 code to demonstrate working of # Ways to concatenate tuples # using + operator # initialize tuples test_tup1 = (1, 3, 5) test_tup2 = (4, 6) # printing original tuples print("The original tuple 1 : " + str(test_tup1)) print("The original tuple 2 : " + str(test_tup2)) # Ways to concatenate tuples # using + operator res = test_tup1 + test_tup2 # printing result print("The tuple after concatenation is : " + str(res)) |
The original tuple 1 : (1, 3, 5) The original tuple 2 : (4, 6) The tuple after concatenation is : (1, 3, 5, 4, 6)
Method #2 : Using sum()
This is yet another way in which this task can be performed. In this, we add the tuples to one other using sum function.
# Python3 code to demonstrate working of # Ways to concatenate tuples # using sum() # initialize tuples test_tup1 = (1, 3, 5) test_tup2 = (4, 6) # printing original tuples print("The original tuple 1 : " + str(test_tup1)) print("The original tuple 2 : " + str(test_tup2)) # Ways to concatenate tuples # using sum() res = sum((test_tup1, test_tup2), ()) # printing result print("The tuple after concatenation is : " + str(res)) |
The original tuple 1 : (1, 3, 5) The original tuple 2 : (4, 6) The tuple after concatenation is : (1, 3, 5, 4, 6)
Recommended Posts:
- Python | How to Concatenate tuples to nested tuples
- Python | Ways to concatenate two lists
- Python | Ways to concatenate boolean to string
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Find the tuples containing the given element from a list of tuples
- Python | Convert string tuples to list tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Concatenate All Records
- Python | Concatenate dictionary value lists
- Python - Group concatenate Till K
- Python | Numpy np.ma.concatenate() method
- Concatenate images using OpenCV in Python
- numpy.concatenate() function | Python
- Python | Pandas Series.str.cat() to concatenate string
- Python - Concatenate Kth element in Tuple List
- Python - Concatenate string rows in Matrix
- Python | Concatenate two lists element-wise
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

