ggupta
ggupta

Reputation: 717

time efficient way to loop over nested dictionary in python 3

i have one nested dictionary, sample is dict = {'exp_0': {'A':obj, 'B':obj}, 'exp_1': {'A': obj, 'C':obj, 'D':obj}, 'exp_2': {'E':obj, 'C': obj, 'F':obj}}

The obj is the object of a class, i need to call a function of obj for each and every element. so i wrote below piece of code

for expr in dict:
    for k, v in dict[expr].items():
        v.somefunction() ## some function is in obj class

def somefunction():
    # this class has self.id variable
    inst = {"a": self.a, "b":self.b}
    try:
        with open("test", "w") as ffile:
            json.dump(inst, ffile)
        print("success for %s" %(self.id))
    except:
        print("Failre for %s" %(self.id))

This code is working fine, the problem is, it's taking a huge lot of time, let's say if nested dictionary {'A': obj, 'B': obj, ...} has 500 elements, then it is taking 8-9 seconds

Is there any time efficient way to do this?

Upvotes: 1

Views: 87

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213411

The time probably is not taken by dictionary iteration. The actual time might be due to opening and closing of files multiple times in the for loop and doing json.dump() also in for loop. Since somefunction() is getting called from inside nested loop.

You should rather collect your data in an intermediate collection. Then open the file once, write list of values, and then close the file at the end (again, only once). It should improve the performance.

Multiple calls to json.dump() could also be an issue.

In these cases, it's always better to transform your code to perform operation in bulk. That would get you perf improvement.

Upvotes: 1

Related Questions