Python - K length Concatenate Single Valued Tuple
Last Updated :
28 Apr, 2023
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform concatenation of single values tuples, to make them into groups of a bigger size. This kind of problem can occur in web development and day-day programming. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, ), (7, ), (1, )], K = 4
Output : [(3, 6, 8, 2), (9, 4, 7, 1)]
Input : test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, ), (7, ), (1, )], K = 2
Output : [(3, 6), (8, 2), (9, 4), (7, 1)]
Method #1 : Using zip() + list comprehension
The combination of the above functions can be used to solve this problem. In this, we perform the task of forming groups using zip() and constructing values using list comprehension. The size of K can only be limited to 2.
Python3
# Python3 code to demonstrate working of
# K length Concatenate Single Valued Tuple
# Using zip() + list comprehension
# initializing lists
test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
# printing original list
print("The original list is : " + str(test_list))
# K length Concatenate Single Valued Tuple
# Using zip() + list comprehension
res = [a + b for a, b in zip(test_list[::2], test_list[1::2])]
# printing result
print("Concatenated tuples : " + str(res))
Output : The original list is : [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
Concatenated tuples : [(3, 6), (8, 2), (9, 4)]
Time complexity: O(N), where N is the length of the input list.
Auxiliary space: O(N), where N is the length of the input list. This is because the program creates a new list res to store the concatenated tuples, which could potentially be as large as the input list.
Method #2 : Using zip_longest() + chain.from_iterables()
The combination of the above functions can be used to solve this problem. In this, we perform the task of concatenation using zip_longest(), and chain.from_iterables() is used to flatten tuples to list elements before concatenation.
Python3
# Python3 code to demonstrate working of
# K length Concatenate Single Valued Tuple
# Using zip_longest() + chain.from_iterables()
from itertools import chain, zip_longest
# initializing lists
test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# K length Concatenate Single Valued Tuple
# Using zip() + list comprehension
temp = [iter(chain.from_iterable(test_list))] * K
res = list(zip_longest(*temp))
# printing result
print("Concatenated tuples : " + str(res))
Output : The original list is : [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
Concatenated tuples : [(3, 6, 8), (2, 9, 4)]
Time complexity: O(nK), where n is the length of the input list and K is the value of K.
Auxiliary space: O(nK)
Method 3: Using a for loop + append() method
Algorithm:
- Initialize an empty list res to store the concatenated tuples.
- Iterate over the test_list from 0 to the length of test_list by 2 steps.
- Concatenate the tuple at the current index with the tuple at the next index and append it to the res list.
- Print the original test_list and the res list.
Python3
test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
res = []
for i in range(0, len(test_list), 2):
res.append(test_list[i] + test_list[i+1])
print("The original list is : " + str(test_list))
print("Concatenated tuples : " + str(res))
OutputThe original list is : [(3,), (6,), (8,), (2,), (9,), (4,)]
Concatenated tuples : [(3, 6), (8, 2), (9, 4)]
Time Complexity: O(n/2) = O(n), where n is the length of the test_list.
The for loop iterates n/2 times, as it iterates over the test_list by 2 steps at a time. The concatenation of two tuples takes constant time.
Auxiliary Space: O(n/2) = O(n), where n is the length of the test_list.
The space required to store the res list is proportional to the length of the test_list.
Note: This time complexity and space complexity assumes that the length of the test_list is even. If the length of the test_list is odd, the last tuple in the test_list will be ignored as there won't be any tuple to concatenate it with.
Method #4: Using a generator function and itertools.islice()
Python3
# Importing necessary libraries
import itertools
# Define a generator function to yield consecutive tuples of given size
def consecutive_tuples(seq, size):
it = iter(seq)
while True:
tup = tuple(itertools.islice(it, size))
if len(tup) == size:
yield tup
else:
return
# Initializing lists
test_list = [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )]
# Printing original list
print("The original list is : " + str(test_list))
# K length Concatenate Single Valued Tuple
# Using a generator function and itertools.islice()
res = [sum(tup, ()) for tup in consecutive_tuples(test_list, 2)]
# Printing result
print("Concatenated tuples : " + str(res))
OutputThe original list is : [(3,), (6,), (8,), (2,), (9,), (4,)]
Concatenated tuples : [(3, 6), (8, 2), (9, 4)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the size of the consecutive tuples to be formed.
Similar Reads
Python | Ways to concatenate tuples 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 + opera
6 min read
Python | Convert tuple to float value Sometimes, while working with tuple, we can have a problem in which, we need to convert a tuple to floating-point number in which first element represents integer part and next element represents a decimal part. Let's discuss certain way in which this can be achieved. Method : Using join() + float()
3 min read
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 di
6 min read
Python - Concatenate Kth element in Tuple List While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print a specific information from the tuple. For instance, a piece of code would want just names to be printed of all the student data in concatenated format. Lets discuss certain ways how o
8 min read
Python - Concatenate Tuple elements by delimiter Given a tuple, concatenate each element of tuple by delimiter. Input : test_tup = ("Gfg", "is", 4, "Best"), delim = ", " Output : Gfg, is, 4, Best Explanation : Elements joined by ", ". Input : test_tup = ("Gfg", "is", 4), delim = ", " Output : Gfg, is, 4 Explanation : Elements joined by ", ". Metho
7 min read