More handy tools

INPUT · Slides

Laying two sets over each other

01 / 07

What the double loop was for

You want to find the people who appear on both of two lists. You can already write it.

Loop over group A on the outside, group B on the inside, and add the name whenever the two match. The double loop from chapter 2.

It works, but it takes a moment to read. What the code is trying to do does not show in its shape.

group_a = ["Yui", "Haruto", "Minato"]group_b = ["Haruto", "Sora", "Minato"]both = []for a in group_a:    for b in group_b:        if a == b:            both.append(a)print(both)

Result

['Haruto', 'Minato']

02 / 07

What is in both goes with &

Between two sets, a single symbol does it. & (and) returns a new set of the things that are in both.

Seven lines became one. And because the & itself stands for the overlap, what you are after shows the moment you look at it.

The result is a set as well, so do not forget the sorted when you show it.

group_a = {"Yui", "Haruto", "Minato"}group_b = {"Haruto", "Sora", "Minato"}print(sorted(group_a & group_b))

Result

['Haruto', 'Minato']

03 / 07

What is in either goes with |

| (a pipe) gathers up everything in either one. Anything in both collapses into one, naturally.

It is what you want when you put two lists together and ask "not how many turns that is, but how many people there actually are".

group_a = {"Yui", "Haruto", "Minato"}group_b = {"Haruto", "Sora", "Minato"}print(sorted(group_a | group_b))print(len(group_a | group_b))

Result

['Haruto', 'Minato', 'Sora', 'Yui']
4

04 / 07

What is on one side only goes with -

- (a minus) is "in the left and not in the right". The same symbol as subtraction, and the point of it is that it has a direction.

a - b and b - a are different things. See it for yourself below.

group_a = {"Yui", "Haruto", "Minato"}group_b = {"Haruto", "Sora", "Minato"}print(sorted(group_a - group_b))print(sorted(group_b - group_a))

Result

['Yui']
['Sora']

05 / 07

One or the other but not both goes with ^

^ (a caret) is "in one or the other but not both". Anything in both drops out.

Think of it as a - b and b - a added together and it falls into place. It is the symbol you reach for when hunting "the places where the two disagree".

group_a = {"Yui", "Haruto", "Minato"}group_b = {"Haruto", "Sora", "Minato"}print(sorted(group_a ^ group_b))

Result

['Sora', 'Yui']

06 / 07

The four, side by side

Those four symbols are all there is. With a and b as sets,

  • a & b … in both
  • a | b … in either
  • a - b … in a and not in b
  • a ^ b … in one or the other but not both

Every one of them returns a new set. Nothing inside a or b changes, so you can go on using them as often as you like.

a = {1, 2, 3}b = {2, 3, 4}print(sorted(a & b))print(sorted(a | b))print(sorted(a - b))print(sorted(a ^ b))

Result

[2, 3]
[1, 2, 3, 4]
[1]
[1, 4]

07 / 07

Put it to work on the shopping

Take "what is already in the house" away from "what to buy" and only the things you really have to buy are left.

Working out a difference or an overlap like this comes up astonishingly often once you are writing programs. Try it with lists and it is a double loop; with sets it is one symbol.

From the next lesson we move on to tools for counting and grouping.

shopping = {"eggs", "milk", "bread"}fridge = {"milk", "butter"}print(sorted(shopping - fridge))

Result

['bread', 'eggs']