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
test_list = [( 3 , ), ( 6 , ), ( 8 , ), ( 2 , ), ( 9 , ), ( 4 , )]
print ( "The original list is : " + str (test_list))
res = [a + b for a, b in zip (test_list[:: 2 ], test_list[ 1 :: 2 ])]
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
from itertools import chain, zip_longest
test_list = [( 3 , ), ( 6 , ), ( 8 , ), ( 2 , ), ( 9 , ), ( 4 , )]
print ( "The original list is : " + str (test_list))
K = 3
temp = [ iter (chain.from_iterable(test_list))] * K
res = list (zip_longest( * temp))
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))
|
Output
The 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
import itertools
def consecutive_tuples(seq, size):
it = iter (seq)
while True :
tup = tuple (itertools.islice(it, size))
if len (tup) = = size:
yield tup
else :
return
test_list = [( 3 , ), ( 6 , ), ( 8 , ), ( 2 , ), ( 9 , ), ( 4 , )]
print ( "The original list is : " + str (test_list))
res = [ sum (tup, ()) for tup in consecutive_tuples(test_list, 2 )]
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(k), where k is the size of the consecutive tuples to be formed.
Last Updated :
28 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...