Python – K length Concatenate Single Valued Tuple
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 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 above functions can be used to solve this problem. In this, we perform the task of forming groups using zip() and construction of 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)) |
The original list is : [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )] Concatenated tuples : [(3, 6), (8, 2), (9, 4)]
Method #2 : Using zip_longest() + chain.from_iterables()
The combination of 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)) |
The original list is : [(3, ), (6, ), (8, ), (2, ), (9, ), (4, )] Concatenated tuples : [(3, 6, 8), (2, 9, 4)]