How to Process Data in Batches in Python

Search for a command to run...

Besides presenting a nice technique for data processing -My, my! Back in the 70's I used to work with batch jobs using punched card stacks-, you also made me discover a very nice open source library, tqdm, that is (was, really) an undiscovered treasure for my toolbox. Keep up the nice work, Nithish!
Thanks for your kind words. I can imagine your pain working with the batch jobs using punched cards. tqdm is definitely one of the nice libraries that you just cannot stop using once you learn about it :)
There are several hackathons on a wide variety of subjects every week. As a new person in the hackathon atmosphere, it could be quite overwhelming to attend a hackathon. In this article, I will summarize some of the things that I learned over the pas...

As part of the Hashnode Technical Writing Bootcamp, we were asked to write about why we blog. This would be the reference when facing Writer's block. My main motivations for blogging are like the ones I mentioned in my article on successful blogging...

We spend a lot of time as engineers on our terminals. Here is a list of 6 command-line utilities that can help you boost your productivity on the terminal. 1. Autojump (j) Autojump is a fast way to navigate your filesystem. It keeps track of the dir...

I have recently been trying to write more by participating in the Hashnode Technical Writing Bootcamp. As part of one of our sessions, we were asked to think about what successful blogging means to each of us & write about it. Before I start going ...

There are some situations when you have a huge list of items to process but you cannot do them in one go due to some limitations of the systems that process the list.
Some examples:
long_list = list(range(100))
sub_list_length = 10
sub_lists = [
long_list[i : i + sub_list_length]
for i in range(0, len(long_list), sub_list_length)
]
Let us try to break down the code
long_list is a list of 100 numbers. We are splitting this list of numbers into sub lists specified by the sub_list_length of 10. The list comprehension is relying on slices of the original list.
print(long_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
print(sub_lists)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]
print(list(range(0, len(long_list), sub_list_length)))
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
results = []
for sub_list in sub_lists:
partial_result = process_function(sub_list)
results.append(partial_result)
Here process_function can be any function that processes the lists. In our example, this would be the call to the API or sub processes that process the lists.
Additionally, you can keep track of the progress of the process by wrapping up the for loop iterable using an open source library, tqdm to display a progress bar that also indicates how long each iteration takes. It works for any iterable as well.
from tqdm import tqdm
results = []
for sub_list in tqdm(sub_lists):
partial_result = process_function(sub_list)
results.append(partial_result)

This has come in quite handy for me in quite a few cases.
Cover Photo from CHUTTERSNAP on Unsplash