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.

0 / 104 solved

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

  1. 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
  2. 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

  1. 97Count how many of each there areCounter is a dictionary built for counting. The counting code you wrote in Chapter 2 shrinks to a single line.Go to the exercises
  2. 98A starting value for a key that is not there yetWith defaultdict you never have to write if key not in d: again.Go to the exercises

Patterns in text

  1. 99Search by shapeWith import re you can look for text by its shape — "three digits", say — rather than by its exact words.Go to the exercises
  2. 100Pull the pieces out with bracketsWrap part of a shape in ( ) and take just that back with group(1). This is taking a match apart.Go to the exercises
  3. 101Replace by patternre.sub replaces by shape rather than by exact text. It does what replace could not.Go to the exercises

Call itself

  1. 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

Next up is “Writing it cleanly”. From code that runs to code that gets read. Write for yourself in three days, and for the person next to you.