Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking progress in python list(map(...))

I am attempting to track progress in the following code:

from toolz import compose
calculator = compose(my_function, list, my_dict.get, tuple)
result = list(zip(*map(calculator, my_values)))

my_values is a list of length ~1mio. My first attempt is to add a counter to my_function that increments and print it out when a multiple of X (e.g. X==500) is reached.

Is there a pythonic or cleaner way to achieve this, i.e. without adding lots of counters to various loops? A progress bar in jupyter notebook would work too.

like image 898
jpp Avatar asked Oct 16 '25 20:10

jpp


1 Answers

If a progress bar in Jupyter will work, I like to use tqdm, as it works for any iterable. Here is some sample code (slightly simplified from your example since I had to write my_function, my_values, etc):

def my_function(x):
    yield x + 2

my_values = range(1000000)

result = list(zip(*map(my_function, my_values))) 

Now just add tqdm on my_values (no progress checkers/counters clogging up your code!) to get a nice progress bar:

from tqdm import tqdm

def my_function(x):
    yield x + 2

my_values = tqdm(range(1000000))

result = list(zip(*map(my_function, my_values)))

which rolls through the awesome tqdm progress bar:

100%|██████████| 1000000/1000000 [00:04<00:00, 210661.41it/s]

Note I have nothing to do with the tqdm project; I just like using it. https://github.com/tqdm/tqdm

like image 196
muskrat Avatar answered Oct 18 '25 17:10

muskrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!