CHAPTER 11 · 8 lessons · 104 exercises
More handy tools
Sets, collections, regular expressions and recursion. Tools that suddenly make life easier, each with the reason you would want it.
This chapter has a different character from the rest. Instead of following one thread, it gathers up tools that suddenly make life easier once you know them. You could write without every one of them, and having them is night and day.
First, the set. Like a list, except it cannot hold the same value twice. If all you want is to drop duplicates, set(numbers) does it in one word. It also comes with operations for what two sets share and what only one of them has, so "the students enrolled in both classes" becomes a single line. No more writing the double loop yourself.
Next, collections, one of the most-used modules in the standard library. Counter counts how many of each thing there are, which turns counting word frequencies into genuinely one line. defaultdict is a dictionary that quietly supplies a starting value when you touch a key that is not there yet, so you stop writing if key not in d: over and over.
Then regular expressions, the machinery for finding "something shaped like this" inside text. Phone numbers, postcodes, dates. You search by a pattern, such as "three digits, a hyphen, four digits". It looks like a pile of punctuation and it does startle people, so we cover only a handful of basics. You do not need all of it — knowing such a thing exists, and being able to look it up when you need it, is plenty.
Last is recursion, where a function calls itself. It is less a practical tool than a way of thinking: break a big problem into a smaller problem of the same shape. It comes into its own with anything nested, like walking through folders inside folders.
Every tool here is one you can live without. Which is exactly why if nobody tells you, you never use them. Take away the names, and a sense of when their moment comes.
Lessons in this chapter
Handle overlap
- 95A container that keeps one of eachA set cannot hold the same value twice. "Take the duplicates out" comes down to
set(numbers)and nothing more.Go to the exercises - 96Laying two sets over each otherFour of them:
&|-^. Finding "the people on both lists" no longer wants a double loop.Go to the exercises
Count and group
- 97Count how many of each there are
Counteris a dictionary built for counting. The counting code you wrote in Chapter 2 shrinks to a single line.Go to the exercises - 98A starting value for a key that is not there yetWith
defaultdictyou never have to writeif key not in d:again.Go to the exercises
Patterns in text
- 99Search by shapeWith
import reyou can look for text by its shape — "three digits", say — rather than by its exact words.Go to the exercises - 100Pull the pieces out with bracketsWrap part of a shape in
( )and take just that back withgroup(1). This is taking a match apart.Go to the exercises - 101Replace by pattern
re.subreplaces by shape rather than by exact text. It does whatreplacecould not.Go to the exercises
Call itself
- 102A function that calls itselfA function calls itself from inside itself. Writing the stopping condition first is the whole of it.Go to the exercises