Open In App

Python | Ways to concatenate tuples

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

step-by-step approach :

  1. Initialize two tuples with elements (1, 3, 5) and (4, 6) respectively and assign them to the variables test_tup1 and test_tup2.
  2. Print the original values of the two tuples using print() function and concatenate them with the string using + operator.
  3. Concatenate the two tuples test_tup1 and test_tup2 using + operator and assign the result to a new variable res.
  4. Print the concatenated tuple res using print() function and concatenate it with the string using + operator.
  5. End of the program.

Python3




# 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))


Output : 

The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)

Time Complexity: O(N)
Auxiliary Space: O(N) where N is the lenght of the concatenate string.

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




# 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))


Output : 

The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)

Time complexity: O(n), where n is the total number of elements in both tuples.
Auxiliary space: O(n), where n is the total number of elements in both tuples. 

Method #3: Using list() and extend() methods

Python3




# Python3 code to demonstrate working of
# Ways to concatenate tuples
 
# 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
x = list(test_tup1)
y = list(test_tup2)
x.extend(y)
res = tuple(x)
 
# printing result
print("The tuple after concatenation is : " + str(res))


Output

The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)

Method #4: Using itertools.chain() function

Algorithm

1. Import the itertools module.
2. Create the original tuples tuple1 and tuple2.
3. Use the itertools.chain() function to concatenate the tuples.
4. Convert the concatenated result to a tuple.
5. Print the concatenated tuple.

Python3




# Python program to concatenate tuples
 
import itertools
 
# original tuples
tuple1 = (1, 3, 5)
tuple2 = (4, 6)
 
# concatenate the tuples using itertools.chain()
tuple3 = tuple(itertools.chain(tuple1, tuple2))
 
# print the concatenated tuple
print("Concatenated tuple:", tuple3)


Output

Concatenated tuple: (1, 3, 5, 4, 6)

Time Complexity: O(len(tuple1) + len(tuple2)), The itertools.chain() function is a lazy iterator that does not actually create a new list or tuple. Instead, it yields the elements from each of the input iterables in sequence. Therefore, the time complexity of the itertools.chain() function is O(1), because it only takes a constant amount of time to set up the iterator. The conversion to a tuple takes linear time in the length of the concatenated iterable, which is O(len(tuple1) + len(tuple2)). 

Auxiliary Space: O(len(tuple1) + len(tuple2)), because the itertools.chain() function does not create a new list or tuple, but only an iterator that yields the elements in sequence. The tuple() function creates a new tuple that stores the concatenated elements. Therefore, the space complexity of the approach is proportional to the size of the concatenated tuple.

Method #5: Using the tuple() constructor and the * operator

This method uses the tuple() constructor to create a new tuple by concatenating the two original tuples using the + operator, and then converts the result back to a tuple using the tuple() constructor.

Python3




# 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 and tuple() constructor
res = tuple(test_tup1 + test_tup2)
 
# printing result
print("The tuple after concatenation is : " + str(res))


Output

The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)

Time complexity: O(n) ,where n is the total size of the tuples being concatenated. 
Auxiliary space: O(n), where n is the total size of the tuples being concatenated. 

METHOD 6: Using the += operator and the += method:

APPROACH:

In this approach, we use the += operator to concatenate the second tuple to the first tuple. This modifies the first tuple in place. We can also use the += method of tuples to achieve the same result.

ALGORITHM:

1.Create the first tuple t1.
2.Create the second tuple t2.
3.Use the += operator to concatenate t2 to t1.
4.Print the concatenated tuple t1

Python3




t1 = (1, 2, 3)
t2 = (4, 5, 6)
t1 += t2
print(t1)  # Output: (1, 2, 3, 4, 5, 6)


Output

(1, 2, 3, 4, 5, 6)

O(n) time complexity and O(n) auxiliary space.

Approach#7: Using reduce

Use a lambda function with reduce() function to concatenate two tuples.

Algorithm

1. Define a lambda function that takes two tuples as input and reduces them by concatenation using the + operator.
2. Call the lambda function with the two input tuples as arguments using the reduce() function.
3. Return the concatenated tuple.

Python3




from functools import reduce
concat_tuples = lambda t1, t2: reduce(lambda x, y: x + y, [t1, t2])
t1 = (1, 3, 5)
t2 = (4, 6)
concatenated_tuple = concat_tuples(t1, t2)
print(concatenated_tuple)


Output

(1, 3, 5, 4, 6)

Time Complexity: O(n) where n is the total number of elements in the two tuples being concatenated.
Space Complexity: O(n) where n is the total number of elements in the two tuples being concatenated.



Previous Article
Next Article

Similar Reads

Python | How to Concatenate tuples to nested tuples
Sometimes, while working with tuples, we can have a problem in which we need to convert individual records into a nested collection yet remaining as separate element. Usual addition of tuples, generally adds the contents and hence flattens the resultant container, this is usually undesired. Let's discuss certain ways in which this problem is solved
6 min read
Python - Concatenate Maximum Tuples
Given a tuple list with string and its magnitude, the task is to write a python program to join all the strings with maximum magnitudes. Examples: Input : test_list = [("Gfg is best", 8), ("gfg is good", 7), ("for", 2), ("for all geeks", 8)]Output : "Gfg is best for all geeks" Explanation : 8 is maximum tuple element and concatenation of keys yield
8 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read
Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Practice Tags :