Example:
tuple = ("python", "includehelp", 43, 54.23)
Chunk tuples to N size
In this article, we will create a Python program to create chunk tuples of size N from the given tuple.
Input:
(4, 2, 7, 6, 1, 12, 54, 76, 87, 99)
N = 2
Output:
(4, 2), (7, 6), (1, 12), (54, 76), (87, 99)
We need to create chunks of size N, for which will iterate the tuple and take each chunk of size N and create a tuple using it.
Method 1:
One method to solve the problem is by using a direct method. In which, we will iterate the tuple and use another count for size N values to be feeding in new chunk tuples.
Output:
Method 2:
Another approach to solving the problem is by using iter() method to create an iterator of size N, which then we converted into tuple using zip() method.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer