Python | Split tuple into groups of n
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
# 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
# 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 receipes
# 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))
Recommended Posts:
- Python | Split string in groups of n consecutive characters
- Python | Pandas Split strings into two List/Columns using str.split()
- Unpacking a Tuple in Python
- Python | Reversing a Tuple
- Python | tuple() Function
- Python | Convert a list into a tuple
- Python | Find whether all tuple have same length
- Python | Removing strings from tuple
- Python | Unpacking tuple of lists
- Python | Count occurrences of an element in a Tuple
- Python | Sort a tuple by its float element
- Python | Join tuple elements in a list
- Python | Find number of lists in a tuple
- Python | Program to convert a tuple to a string
- Python | Convert a list into tuple of lists
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.



