Pythonic Programming

tips and tricks for productive use of the python language's properties

Get Started. It's Free
or sign up with your email address
Rocket clouds
Pythonic Programming by Mind Map: Pythonic Programming

1. Syntax

1.1. use parenthesis for long-line wrapping

1.2. use r"C:\temp.txt" for raw strings

1.2.1. Backslashes not interpreted as escapes

1.2.2. ideal for Windows file paths

1.3. use "".join(list) to join a list of strings

1.3.1. add spaces or commas as needed " ".join(list) ", ".join(list)

1.3.2. use generator functions "".join(fn(i) for i in items)

1.4. use collections.defaultdict() to create dictionaries with default factory functions

1.5. use locals() for named string formatting

1.6. Use a list comprehension when a computed list is the desired end result.

1.6.1. [n ** 2 for n in range(10) if n % 2]

1.6.2. all values calculated at once

1.6.3. greedy evaluation

1.7. Use a generator expression when the computed list is just an intermediate step.

1.7.1. values calculated as needed

1.8. use lists instead of concatenating strings using the "+=" operator.

1.8.1. huge memory saver

1.8.2. huge speed improvement

1.8.3. can be joined before output

2. Links

2.1. Code Like a Pythonista

2.2. Idioms and Anti-Idioms in Python

2.3. Python Idioms and Efficiency Suggestions