Sometimes, while working with data, we can have a problem in which we may need to perform chunking of tuples each of size N. This is popular in applications in which we need to supply data in chunks. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This is brute and shorthand method to perform this task. In this, we split the N elements at a time and construct a new tuple for them.
Python3
| # Python3 code to demonstrate working of# Chunk Tuples to N# using list comprehension# initialize tupletest_tup =(10, 4, 5, 6, 7, 6, 8, 3, 4)# printing original tupleprint("The original tuple : "+str(test_tup))# initialize N N =3# Chunk Tuples to N# using list comprehensionres =[test_tup[i : i +N] fori inrange(0, len(test_tup), N)]# printing resultprint("The tuples after chunking are : "+str(res)) | 
The original tuple : (10, 4, 5, 6, 7, 6, 8, 3, 4) The tuples after chunking are : [(10, 4, 5), (6, 7, 6), (8, 3, 4)]
Method #2 : Using zip() + iter() The combination of above functions can also be used to solve this problem. In this, we use zip() to combine chunks and iter() converts to suitable format.
Python3
| # Python3 code to demonstrate working of# Chunk Tuples to N# using zip() + iter()# initialize tupletest_tup =(10, 4, 5, 6, 7, 6, 8, 3, 4)# printing original tupleprint("The original tuple : "+str(test_tup))# initialize N N =3# Chunk Tuples to N# using zip() + iter()temp =[iter(test_tup)] *Nres =list(zip(*temp))# printing resultprint("The tuples after chunking are : "+str(res)) | 
The original tuple : (10, 4, 5, 6, 7, 6, 8, 3, 4) The tuples after chunking are : [(10, 4, 5), (6, 7, 6), (8, 3, 4)]
Method #3: Using numpy
Note: Install numpy module using command “pip install numpy”
The numpy library in python provides a function called numpy.array_split() which can be used to perform chunking of tuples each of size N.
Python3
| importnumpy as np# initialize tupletest_tup =(10, 4, 5, 6, 7, 6, 8, 3, 4)# printing original tupleprint("The original tuple : "+str(test_tup))# initialize NN =3# Chunk Tuples to N using numpyres =np.array_split(test_tup, len(test_tup)/N)# printing resultprint("The tuples after chunking are : "+str(res))#This code is contributed by Edula Vinay Kumar Reddy | 
Output:
The original tuple : (10, 4, 5, 6, 7, 6, 8, 3, 4)
The tuples after chunking are : [array([10,  4,  5]), array([6, 7, 6]), array([8, 3, 4])]
 
Time complexity: O(n) where n is the size of the tuple. 
Auxiliary Space: O(n)
Method #4: Using itertools.islice():
Python3
| importitertoolsdefchunk(iterable, size):    it =iter(iterable)    returniter(lambda: tuple(itertools.islice(it, size)), ())test_tup =(10, 4, 5, 6, 7, 6, 8, 3, 4)# printing original tupleprint("The original tuple : "+str(test_tup))N =3res =[x forx inchunk(test_tup, N)]print("The tuples after chunking are : "+str(res))#This code is contributed by Jyothi pinjala. | 
The original tuple : (10, 4, 5, 6, 7, 6, 8, 3, 4) The tuples after chunking are : [(10, 4, 5), (6, 7, 6), (8, 3, 4)]
Time complexity: O(n)  
Auxiliary Space: O(n)
Method #6: Using recursion
You can also chunk an iterable using recursion. This approach may not be as efficient as some of the other methods, but it can be a good exercise in recursion.
step-by-step approach:
- Define a function called “chunk” that takes two arguments: an iterable and a chunk size.
- If the length of the iterable is less than or equal to the chunk size, return a list containing the iterable.
- Otherwise, use recursion to split the iterable into chunks of size “size”.
- Append the first chunk to a list called “chunks”.
- Return the “chunks” list.
Python3
| defchunk(iterable, size):    iflen(iterable) <=size:        return[iterable]    else:        chunks =chunk(iterable[size:], size)        chunks.insert(0, iterable[:size])        returnchunkstest_tup =(10, 4, 5, 6, 7, 6, 8, 3, 4)# printing original tupleprint("The original tuple : "+str(test_tup))N =3res =chunk(test_tup, N)print("The tuples after chunking are : "+str(res)) | 
The original tuple : (10, 4, 5, 6, 7, 6, 8, 3, 4) The tuples after chunking are : [(10, 4, 5), (6, 7, 6), (8, 3, 4)]
Time complexity: O(n log(n)), where n is the length of the iterable .
Auxiliary space: O(n log(n)), where n is the length of the iterable .

 
                                    







