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
How to split Python tuples into sub-tuples?
In this article, we will show you how to split Python tuples into sub-tuples of equal size. Tuples are immutable, ordered collections in Python that can be divided into smaller chunks using various methods.
What are Tuples?
Tuples are immutable, ordered data structures used to store collections in Python. Unlike lists which are mutable and have variable length, tuples have a fixed length once created and cannot be modified.
Method 1: Using Slicing
The most straightforward approach uses slice notation with the range() function to create sub-tuples of a specific size.
Example
The following program splits a tuple into sub-tuples of 3 elements each using slicing ?
# input tuple
input_tuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
# printing input tuple
print("Input Tuple:", input_tuple)
# number of elements in each sub-tuple
n = 3
# splitting the input tuple with n(3) elements
# looping from 0 to length of tuple with n as step value
result_tuple = tuple(input_tuple[i:i + n] for i in range(0, len(input_tuple), n))
# printing the result tuple
print("Splitting into sub-tuples with 3 elements each:")
print(result_tuple)
Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) Splitting into sub-tuples with 3 elements each: ((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))
Method 2: Using enumerate() and Modulo Operator
The enumerate() function adds indices to tuple elements, and the modulo operator (%) helps identify positions where new sub-tuples should start.
Example
The following program uses enumerate() and modulo operator to split tuples ?
# input tuple
input_tuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
# printing input tuple
print("Input Tuple:", input_tuple)
# splitting using enumerate and modulo operator
# only start new sub-tuple when index is divisible by 3
result_tuple = tuple(input_tuple[i:i + 3] for i, k in enumerate(input_tuple)
if i % 3 == 0)
# printing the result tuple
print("Splitting into sub-tuples with 3 elements each:")
print(result_tuple)
Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) Splitting into sub-tuples with 3 elements each: ((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))
Method 3: Using iter() and zip() Functions
This approach uses iterator techniques to efficiently split tuples by creating multiple iterators and combining them with zip().
Example
The following program splits tuples using iter() and zip() functions ?
# creating a function to split the input tuple into groups of k elements
def split_tuple_and_group(k, input_tuple):
# create k identical iterators from the tuple
args = [iter(input_tuple)] * k
# zip them together to create groups of k elements
return zip(*args)
# input tuple
input_tuple = (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7)
# printing input tuple
print("Input Tuple:", input_tuple)
# calling the split_tuple_and_group function
result_tuple = tuple(split_tuple_and_group(3, input_tuple))
# printing the result tuple
print("Splitting into sub-tuples with 3 elements each:")
print(result_tuple)
Input Tuple: (10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6, 7) Splitting into sub-tuples with 3 elements each: ((10, 20, 30), (40, 50, 1), (2, 3, 4), (5, 6, 7))
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Slicing | High | Good | Simple cases, beginners |
| enumerate() + modulo | Medium | Good | When you need index information |
| iter() + zip() | Medium | Best | Large tuples, memory efficiency |
Conclusion
Use slicing for simple tuple splitting tasks due to its readability. For better memory efficiency with large tuples, prefer the iter() and zip() approach. The enumerate() method is useful when you need both values and indices during processing.
