Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Split tuple into groups of n in Python
When it is required to split a tuple into groups of 'n' elements, list comprehension can be used along with slicing. A tuple is an immutable data type, meaning values once defined cannot be changed by accessing their index elements. If we try to change the elements, it results in an error. Tuples are important containers since they ensure read-only access.
List comprehension is a shorthand to iterate through sequences and perform operations on them efficiently.
Method 1: Using List Comprehension with Slicing
The most common approach uses list comprehension with range() and slicing ?
my_tuple = (12, 34, 32, 41, 56, 78, 9, 0, 87, 53, 12, 45, 12, 6)
print("The tuple is:")
print(my_tuple)
# Split into groups of 3
my_result = tuple(my_tuple[x:x + 3]
for x in range(0, len(my_tuple), 3))
print("The resultant tuple is:")
print(my_result)
The tuple is: (12, 34, 32, 41, 56, 78, 9, 0, 87, 53, 12, 45, 12, 6) The resultant tuple is: ((12, 34, 32), (41, 56, 78), (9, 0, 87), (53, 12, 45), (12, 6))
Method 2: Using a Function for Reusability
Creating a reusable function makes the code more modular ?
def split_tuple(data, group_size):
"""Split tuple into groups of specified size"""
return tuple(data[i:i + group_size]
for i in range(0, len(data), group_size))
# Example with different group sizes
numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
print("Original tuple:", numbers)
print("Groups of 4:", split_tuple(numbers, 4))
print("Groups of 5:", split_tuple(numbers, 5))
Original tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) Groups of 4: ((1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)) Groups of 5: ((1, 2, 3, 4, 5), (6, 7, 8, 9, 10), (11, 12))
Method 3: Using itertools.batched() (Python 3.12+)
Python 3.12 introduced itertools.batched() for this specific purpose ?
import itertools
my_tuple = (12, 34, 32, 41, 56, 78, 9, 0, 87, 53, 12, 45)
# Split into groups of 3 using itertools.batched()
result = tuple(itertools.batched(my_tuple, 3))
print("Using itertools.batched():", result)
How It Works
The list comprehension approach works by:
- range(0, len(my_tuple), 3) generates start indices: 0, 3, 6, 9, etc.
- my_tuple[x:x + 3] creates a slice from index x to x+3
- tuple() converts the generator expression to a tuple of tuples
- If the last group has fewer than n elements, it still forms a valid group
Comparison
| Method | Python Version | Best For |
|---|---|---|
| List comprehension | All versions | Most common, readable |
| Custom function | All versions | Reusability, different sizes |
| itertools.batched() | 3.12+ | Built-in solution, cleaner |
Conclusion
Use list comprehension with slicing for splitting tuples into groups of n elements. For Python 3.12+, itertools.batched() provides a cleaner built-in solution. The remaining elements form a smaller group if the tuple length is not divisible by n.
