0

I have a section of code used to find a key value within a dictionary. The current method i'm using works, however i'm attempting to speed up the execution of the script.

To do this i want to move the if ticker in tick: to immediately after for tick in trades_dic["Ticker"].items():. The intention of which is to speed up this element by removing the need to check all combinations.

Full section of code below, any help would be much appreciated. :)

    for tick in trades_dic["Ticker"].items():
        for stat in trades_dic["Status"].items():
            if ticker in tick and "Open" in stat:
                (tick_k, tick_val) = tick
                (stat_k, stat_val) = stat
                if tick_k == stat_k:
                    (active_k, v) = tick
                    break
        else:
            continue
        break
Tim Pickup
  • 123
  • 7

1 Answers1

0

I'm assuming ticker contains the key you're looking up in in both of your dictionaries -- looping over items() basically defeats the entire purpose of a dictionary structure. You can just look up the value directly by indexing with the key:

tick_val = trades_dic["Ticker"][ticker]
stat_val = trades_dic["Status"][ticker]
if stat_val == "Open":
    # do stuff with tick_val
tzaman
  • 46,925
  • 11
  • 90
  • 115