Given a list of elements, learn to return sublists/chunks of the given size.
Input
lst = [1, 2, 3, 4, 5, 6]
chk_size = 3
Output
[[1,2,3], [4,5,6]]
Input
lst = [1, 2, 3, 4, 5]
chk_size = 3
Output
[[1,2,3], [4,5]]
yield
keywordThe yield
keyword is used to produce a series of values over time.
Refer to Using the keyword Yield in Python to learn more.
In the code below, we iterate over the given list by jumping the list by chunk size in every iteration. In every iteration, we yield
the sublist from the index we’re iterating to the index at the starting of the next chunk.
The chunks
method returns an iterator. We can either convert the iterator to a list
by passing the iterator to list()
or by iterating it until all the elements in the iterator get exhausted.
def chunks(lst, chk_size):for index in range(0, len(lst), chk_size):yield lst[index:index + chk_size]chk_size = 3lst = list(range(15))chunked_lst = list(chunks(lst, chk_size))print("Original List - ", lst)print("Chunked List of chunk size %s - %s" % (chk_size, chunked_lst))
chunks
that accepts a list and chunk size and returns a chunk every time it’s called.chk_size
.lst
.chunks()
method to chunk lst
into chunks of size, chk_size
.List comprehensions are a concise way to create lists. They’re used to create a new list by iterating over another list.
Refer to What is list comprehension in Python? to learn more.
In the code below, we use list comprehension to return sublists/chunks of the given size. Here, we iterate over the given list by jumping the list by chunk size in every iteration. In every iteration, we get the sublist from the index we’re iterating to the index at the start of the next chunk.
lst = list(range(5))chk_size = 4chunks = [lst[i:i + chk_size] for i in range(0, len(lst), chk_size)]print("Original List - ", lst)print("Chunked List of chunk size %s - %s" % (chk_size, chunks))
lst
.chk_size
.lst
into chunks of size, chk_size
.itertools
We can chunk the list into the given sizes using the islice
method of the itertools
module.
Refer to What is itertools.islice() method in Python? to learn more.
We create an iterator from the given list using the iter()
function. Then we use islice
method to chunk the iterator. Using the iter()
method with a lambda and the sentinel value as an empty tuple we return an iterator of chunks.
Refer to How to use iter() in Python to learn more.
from itertools import islicedef chunks(lst, chk_size):lst_it = iter(lst)return iter(lambda: tuple(islice(lst_it, chk_size)), ())lst = list(range(5))chk_size = 4chunked_list = list(chunks(lst, chk_size))print("Original List - ", lst)print("Chunked List of chunk size %s - %s" % (chk_size, chunked_list))