user7186
user7186

Reputation: 449

Python Function Takes too Long as Dictionary Gets too Big

def assign_ID(dic,id):
  for key, value in enumerate(dic):
    for elem in range(value):
      if id in dic[value]:
        return value 

My function above does what I want it to do-- but the problem is that as the dictionary dic gets bigger in the main part of my program, my "assign_ID" function takes too long. Initially this function takes about 1/100 of a second to run, but after a few thousand rows of input, it starts to take half a second, then a full second and then longer. The trouble is that my input file is so large that ultimately this function makes the whole program take at least two full days to run.

Is there a way to re-write the above function so that it runs quicker? I would like to be able to periodically run this program so I really want it to run faster than it does.

Thank you very much in advance for any help!

Upvotes: 1

Views: 2215

Answers (2)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58885

You can use iteritems to iterate over the dictionary items more efficiently (in Python 2.x):

for k,v in dic.iteritems():
    if id in v:
        return k

as observed by @Bakuriu, in Python 3.x:

for k,v in dic.items():
    if id in v:
        return k

since in Python 3.x items() will give an iterator, similarly to iteritems() in Python 2.x.

Upvotes: 4

user2555451
user2555451

Reputation:

Well, straight off the bat, get rid of enumerate. Also, your second for-loop does nothing. Get rid of that too:

def assign_ID(dic, id):
    for key in dic:
        if id in dic[key]:
            return key

The above function should do everything that your old one does, only it will be far faster.

Upvotes: 4

Related Questions