Update docs/writing/style.rst
authorEstevan Pequeno <estevan.pequeno@gmail.com>
Fri, 30 Dec 2011 17:38:13 +0000 (11:38 -0600)
committerEstevan Pequeno <estevan.pequeno@gmail.com>
Fri, 30 Dec 2011 17:38:13 +0000 (11:38 -0600)
docs/writing/style.rst

index e0cf87d..54b4a10 100644 (file)
@@ -14,6 +14,23 @@ A common idiom for creating strings is to use `join <http://docs.python.org/libr
 
 This will set the value of the variable *word* to 'spam'. This idiom can be applied to lists and tuples.
 
+Sometimes we need to search through a collection of things. Let's look at two options: lists and dictionaries.
+
+Take the following code for example::
+    
+    d = {'s': [], 'p': [], 'a': [], 'm': []}
+    l = ['s', 'p', 'a', 'm']
+    
+    def lookup_dict(d):
+        return 's' in d
+
+    def lookup_list(l):
+        return 's' in l
+
+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.
+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.
+For more information see this `StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>`_ page.
+
 Zen of Python
 -------------