Python | Split tuple into groups of n
Last Updated :
13 Mar, 2023
Given a tuple, the task is to divide it into smaller groups of n. Let's discuss a few methods to accomplish a given task.
Method #1: Using enumerate and range function
Python3
# Python code to demonstrate
# how to split tuple
# into the group of k-tuples
# initialising tuple
ini_tuple = (1, 2, 3, 4, 8, 12, 3, 34,
67, 45, 1, 1, 43, 65, 9, 10)
# printing initial tuple
print ("initial list", str(ini_tuple))
# code to group
# tuple into size 4 tuples
res = tuple(ini_tuple[x:x + 4]
for x in range(0, len(ini_tuple), 4))
# printing result
print ("resultant tuples", str(res))
Output:
initial list (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10) resultant tuples ((1, 2, 3, 4), (8, 12, 3, 34), (67, 45, 1, 1), (43, 65, 9, 10))
Method #2: Using enumerate and mod operator
Python3
# Python code to demonstrate
# how to split tuple
# into the group of k-tuples
# initialising tuple
ini_tuple = (1, 2, 3, 4, 8, 12, 3, 34,
67, 45, 1, 1, 43, 65, 9, 10)
# printing initial tuple
print ("initial list", str(ini_tuple))
# code to group
# tuple into size 4 tuples
res = tuple(ini_tuple[n:n + 4] for n, i in enumerate(ini_tuple)
if n % 4 == 0)
# printing result
print ("resultant tuples", str(res))
Output:
initial list (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10) resultant tuples ((1, 2, 3, 4), (8, 12, 3, 34), (67, 45, 1, 1), (43, 65, 9, 10))
Method #3: Using itertools recipes
Python3
# Python code to demonstrate
# how to split tuple
# into the group of k-tuples
# function to group tuple into groups of 4
def grouper(n, iterable):
args = [iter(iterable)] * n
return zip(*args)
# initialising tuple
ini_tuple = (1, 2, 3, 4, 8, 12, 3, 34,
67, 45, 1, 1, 43, 65, 9, 10)
# printing initial tuple
print ("initial list", str(ini_tuple))
# code to group
# tuple into size 4 tuples
res = tuple(grouper(4, ini_tuple))
# printing result
print ("resultant tuples", str(res))
Output:
initial list (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10) resultant tuples ((1, 2, 3, 4), (8, 12, 3, 34), (67, 45, 1, 1), (43, 65, 9, 10))
Method #4: Using zip
Here is another approach using the zip function:
Python3
# initializing tuple
ini_tuple = (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10)
# printing initial tuple
print("initial list", str(ini_tuple))
# code to group tuple into groups of 4
res = tuple(zip(*[iter(ini_tuple)]*4))
# printing result
print("resultant tuples", str(res))
Outputinitial list (1, 2, 3, 4, 8, 12, 3, 34, 67, 45, 1, 1, 43, 65, 9, 10)
resultant tuples ((1, 2, 3, 4), (8, 12, 3, 34), (67, 45, 1, 1), (43, 65, 9, 10))
Time complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Split list in uneven groups Sometimes, while working with python, we can have a problem of splitting a list. This problem is quite common and has many variations. Having solutions to popular variations proves to be good in long run. Let's discuss certain way to split list in uneven groups as defined by other list. Method 1: Us
6 min read
Python - Split in Nested tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform split of elements in nested tuple, by a certain delimiter. This kind of problem can have application in different data domains. Let's discuss certain ways in which this task can be performed. Input : test_
7 min read
Creating Sets of Tuples in Python Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
How To Slice A List Of Tuples In Python? In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
3 min read
Python - Split List on next larger value Given a List, perform split on next larger value. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 11, 2] Output : [[4, 2, 3], [7, 5, 1, 3, 4], [11, 2]] Explanation : After 4, 7 is greater, split happens at that element, and so on. Input : test_list = [4, 2, 3, 7, 5, 1, 3, 4, 1, 2] Output : [[4, 2, 3],
2 min read
Python | Group tuple into list based on value Sometimes, while working with Python tuples, we can have a problem in which we need to group tuple elements to nested list on basis of values allotted to it. This can be useful in many grouping applications. Let's discuss certain ways in which this task can be performed. Method #1 : Using itemgetter
6 min read