@@ -351,6 +351,8 @@ Instead, use a list comprehension:
351351
352352 four_lists = [[] for __ in xrange (4 )]
353353
354+ Create a string from a list
355+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
354356
355357A common idiom for creating strings is to use :py:meth: `str.join ` on an empty
356358string.
@@ -363,30 +365,53 @@ string.
363365 This will set the value of the variable *word * to 'spam'. This idiom can be
364366applied to lists and tuples.
365367
368+ Searching for an item in a collection
369+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
370+
366371Sometimes we need to search through a collection of things. Let's look at two
367- options: lists and dictionaries .
372+ options: lists and sets .
368373
369374Take the following code for example:
370375
371376.. code-block :: python
372377
373- d = { ' s' : [] , ' p' : [] , ' a' : [] , ' m' : []}
378+ s = set ([ ' s' , ' p' , ' a' , ' m' ])
374379 l = [' s' , ' p' , ' a' , ' m' ]
375380
376- def lookup_dict ( d ):
377- return ' s' in d
381+ def lookup_set ( s ):
382+ return ' s' in s
378383
379384 def lookup_list (l ):
380385 return ' s' in l
381386
382- Even though both functions look identical, because *lookup_dict * is utilizing
383- the fact that dictionaries in Python are hashtables, the lookup performance
384- between the two is very different. Python will have to go through each item
385- in the list to find a matching case, which is time consuming. By analysing
386- the hash of the dictionary, finding keys in the dictionary can be done very
387- quickly. For more information see this
387+ Even though both functions look identical, because *lookup_set * is utilizing
388+ the fact that sets in Python are hashtables, the lookup performance
389+ between the two is very different. To determine whether an item is in a list,
390+ Python will have to go through each item until it finds a matching item.
391+ This is time consuming, especially for long lists. In a set, on the other
392+ hand, the hash of the item will tell Python where in the set to look for
393+ a matching item. As a result, the search can be done quickly, even if the
394+ set is large. Searching in dictionaries works the same way. For
395+ more information see this
388396`StackOverflow <http://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table >`_
389- page.
397+ page. For detailed information on the amount of time various common operations
398+ take on each of these data structures, see
399+ `this page <https://wiki.python.org/moin/TimeComplexity? >`_.
400+
401+ Because of these differences in performance, it is often a good idea to use
402+ sets or dictionaries instead of lists in cases where:
403+
404+ * The collection will contain a large number of items
405+
406+ * You will be repeatedly searching for items in the collection
407+
408+ * You do not have duplicate items.
409+
410+ For small collections, or collections which you will not frequently be
411+ searching through, the additional time and memory required to set up the
412+ hashtable will often be greater than the time saved by the improved search
413+ speed.
414+
390415
391416Zen of Python
392417-------------
0 commit comments