Search code examples
pythondictionaryfor-looptimeexecution

Python - Check progress of dictionary creation


I'm creating a dictionary this way:

num_states = 26565000
num_actions = 7

self.P = {
            state: {action: [] for action in range(num_actions)}
            for state in range(num_states)
        }

It's taking quite a bit and I would like to insert print(state) to check the progress of the creation. I'm not sure where to put it.

EDIT:

I need this dictionary beacause I need to involve the whole observation space for an agent I'm trying with reinforcement learning

EDIT.2:

I usually use the regular for loops:

for state in range(num_states)
    print(state)
    for action in range(num_actions)
        ...

I'm not sure how to achieve the same result using this way of coding a for loop.


Solution

  • num_states = 26565000
    num_actions = 7
     
    P = {}
    for state in range(num_states):
      P[state] = {action: [] for action in range(num_actions)}
      if state % 10000 == 0: #print only once on every 1 in 10000
         print(f"\r{state/num_states:.2%}", end="")