Dictionary Methods and Counting Patterns
setdefault, get with a default and the Counter class turn the most common dictionary jobs - counting, grouping and accumulating - into two or three li...
968 published notes across every category.
setdefault, get with a default and the Counter class turn the most common dictionary jobs - counting, grouping and accumulating - into two or three li...
Real data arrives as dictionaries inside dictionaries inside lists. Reaching into it safely, walking it and flattening it are skills worth practising...
A comprehension builds a collection from an expression and a loop, in one line. It reads left to right in the same order as the loop it replaces.
The same syntax as a list comprehension, with a key and a value separated by a colon. It is the fastest way to build, filter, invert or reshape a mapp...
A function packages a piece of work behind a name. def creates it, the call runs it, and return decides what comes back - which is None if you never s...
Positional, keyword, default, variable length and keyword only arguments - five styles, one fixed order, and one famous trap involving mutable default...
Python resolves a name by looking in four places in a fixed order: local, enclosing, global, built-in. Assignment anywhere in a function makes the nam...
A lambda is a small anonymous function written as a single expression. It exists for the places where naming a function would add nothing.
Functions in Python are ordinary objects. They can be stored, passed, returned and built at runtime, and that fact underlies sorting keys, decorators...
A closure is an inner function that keeps hold of variables from the function that created it, long after that function has returned.
A recursive function calls itself on a smaller version of the problem until it reaches a case it can answer directly. Every one needs a base case and...
Fibonacci shows why naive recursion can be catastrophically slow, memoisation fixes it in one line, and some problems are worth the stack frames while...