diff --git a/docs/writing/style.rst b/docs/writing/style.rst index 216c780ce..54b4a10c6 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -7,6 +7,29 @@ Idioms Idiomatic Python code is often referred to as being *pythonic*. +A common idiom for creating strings is to use `join `_ on an empty string.:: + + letters = ['s', 'p', 'a', 'm'] + word = ''.join(letters) + +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 `_ page. Zen of Python -------------