Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the 'in' clause used on python dictionaries call the keys() function every time?

Let's say I have a

dict = {...} #lots of words in dictionary  

I have to do a

for word in ...: #long list of words
    if word in dict:
        #do something

My question is, does the 'if word in dict' call the dict.keys() function every time and hence a lot slower than if I added another variable at the top that is dict_keys = dict.keys()? The result of what I am talking about would be this.

dict = {...}
dict_keys = dict.keys()

for word in ...:
    if word in dict_keys:
        #do something

Thanks

like image 653
apexdodge Avatar asked Feb 01 '11 19:02

apexdodge


People also ask

What is the use of all () in dictionary in Python?

The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty.

Can keys repeat in dictionary Python?

Python dictionary doesn't allow key to be repeated. However, we can use defaultdict to find a workaround.

Which function gets all the keys from a dictionary?

The dict. keys() method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion.

Which of the following is correct about dictionaries in Python?

Q 4 - Which of the following is correct about dictionaries in python? A - Python's dictionaries are kind of hash table type.


1 Answers

no. foo in mydict is in fact a lot faster than foo in keys_list, since dicts are hash tables, so finding the element inside it is O(1). While foo in keys_list would be O(n) (slower as the number of keys grows bigger)

But you could always test yourself:

$ python -m timeit -s "x = range(1000)" "15 in x"
1000000 loops, best of 3: 0.311 usec per loop
$ python -m timeit -s "x = dict.fromkeys(xrange(1000))" "15 in x"
10000000 loops, best of 3: 0.0515 usec per loop

So checking directly on the dict for 1000 elements is one order of magnitude faster, not considering the time of the .keys()

like image 62
nosklo Avatar answered Oct 06 '22 02:10

nosklo