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

Method #2 : Using `zip_longest() + chain.from_iterables()`
The combination of above functions can be used to solve this problem. In this, we perfrom the task of concatenation using zip_longest() and chain.from_iterables() is used to flatten tuples to list elements before concatenation.

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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.