Skip to content

Commit d3dbd65

Browse files
committed
Update docs/writing/style.rst
1 parent b147a1c commit d3dbd65

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/writing/style.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ A common idiom for creating strings is to use `join <http://docs.python.org/libr
1414

1515
This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples.
1616

17+
Sometimes we need to search through a collection of things. Let's look at two options: lists and dictionaries.
18+
19+
Take the following code for example::
20+
21+
d = {'s': [], 'p': [], 'a': [], 'm': []}
22+
l = ['s', 'p', 'a', 'm']
23+
24+
def lookup_dict(d):
25+
return 's' in d
26+
27+
def lookup_list(l):
28+
return 's' in l
29+
30+
Even though both functions look identical, because *lookup_dict* is utilizing the fact that dictionaries in python are hashtables, the lookup performance between the two is very different.
31+
Python will have to go through each item in the list to find a matching case, which is time consuming. By analysing the hash of the dictionary finding keys in the dict can be done very quickly.
32+
For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_ page.
33+
1734
Zen of Python
1835
-------------
1936

0 commit comments

Comments
 (0)