You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/writing/style.rst
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,23 @@ A common idiom for creating strings is to use `join <http://docs.python.org/libr
14
14
15
15
This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples.
16
16
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.
0 commit comments