The Python itertools.batched() function is used to create an iterator that returns batches of a specified size from an iterable. This function is useful for processing data in fixed-size chunks.
If the number of elements in the iterable is not a multiple of the batch size, the final batch will contain the remaining elements.
SyntaxFollowing is the syntax of the Python itertools.batched() function −
itertools.batched(iterable, n)Parameters
This function accepts the following parameters −
This function returns an iterator that yields tuples containing batches of elements from the given iterable.
Example 1Following is an example of the Python itertools.batched() function. Here, we divide a list into batches of size 2 −
import itertools numbers = [1, 2, 3, 4, 5, 6] batches = itertools.batched(numbers, 2) for batch in batches: print(batch)
Following is the output of the above code −
(1, 2) (3, 4) (5, 6)Example 2
Here, we batch a list of strings into groups of 3 elements each using the itertools.batched() function −
import itertools words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"] batches = itertools.batched(words, 3) for batch in batches: print(batch)
Output of the above code is as follows −
('apple', 'banana', 'cherry') ('date', 'elderberry', 'fig') ('grape',)Example 3
Now, we use itertools.batched() function to process a range of numbers in groups of 4 −
import itertools numbers = range(1, 11) batches = itertools.batched(numbers, 4) for batch in batches: print(batch)
The result obtained is as shown below −
(1, 2, 3, 4) (5, 6, 7, 8) (9, 10)Example 4
The itertools.batched() function can also be used to process data in batches while working with files.
Here, we read a list of lines and process them in batches of 2 −
import itertools lines = ["line1", "line2", "line3", "line4", "line5"] batches = itertools.batched(lines, 2) for batch in batches: print(batch)
The result produced is as follows −
('line1', 'line2') ('line3', 'line4') ('line5',)
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4