Comprehensions

INPUT · Slides

The container that drops duplicates

01 / 06

What happens if you drop the colon

Last lesson you learned that braces { } with key: value give you a dictionary.

So what happens if you drop the colon and write only the value? What you get is a set, a different container again.

The headline property of a set is that anything turning up twice is folded into one. In the example below "red" appears twice, and the contents come to three.

words = ["red", "blue", "red", "yellow"]colours = {w for w in words}print(len(colours))

Result

3

02 / 06

Do not show one as it is

A very important warning here. A set has no order.

It does not remember the order things went in, nor sort them. So showing one as it is may not give you the same order every time.

When you want to see the contents, always put it through sorted(...) first. sorted returns a list, so what you see comes out with square brackets round it.

words = ["red", "blue", "red", "yellow"]print(sorted({w for w in words}))

Result

['blue', 'red', 'yellow']

03 / 06

The three containers side by side

Here are three built on the same skeleton, with only the punctuation and the colon changed.

  • [ value for ... ] … a list (ordered, duplicates allowed)
  • { key: value for ... } … a dictionary (looked up by key)
  • { value for ... } … a set (no duplicates, no order)

With braces, a colon means a dictionary and no colon means a set. Tell those apart and you can read any of them.

numbers = [1, 2, 2, 3]print([x for x in numbers])print({x: x * 2 for x in numbers})print(sorted({x for x in numbers}))

Result

[1, 2, 2, 3]
{1: 2, 2: 4, 3: 6}
[1, 2, 3]

04 / 06

Counting and checking are safe

You only need sorted when you are laying the contents out to look at.

  • counting how many with len(...)
  • checking whether something is in there with in

Neither of those has anything to do with order, so use them as they are. In fact those two are most of what a set is for.

words = ["red", "blue", "red"]colours = {w for w in words}print(len(colours))print("red" in colours)print("green" in colours)

Result

2
True
False

05 / 06

You can add an if

An if at the back, the same as ever. The skeleton has not shifted at all.

Take things out, filter them, change them, and let the punctuation decide the container. There is nothing extra to learn since the first lesson of this chapter.

words = ["red", "blue", "red", "yellow"]print(sorted({w for w in words if len(w) <= 4}))

Result

['blue', 'red']

06 / 06

Sets themselves come in Chapter 11

A set also comes with tools of its own, for things like what two sets have in common. Those are all dealt with in Chapter 11.

What to take away from this lesson is the skeleton: change the punctuation and you change the container.

Square brackets for a list, braces with a colon for a dictionary, braces without one for a set. The shape of the writing is the same throughout. Get this far and a comprehension in somebody else's Python will never stop you again.