Open In App

Python – K length Concatenate Single Valued Tuple

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Initialize an empty list res to store the concatenated tuples.
  2. Iterate over the test_list from 0 to the length of test_list by 2 steps.
  3. Concatenate the tuple at the current index with the tuple at the next index and append it to the res list.
  4. 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




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


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
Previous
Next
Share your thoughts in the comments
Similar Reads