Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

Sets

3 min read

Two study sessions, two attendance lists, and some names written twice. Who came at all? Who came to both? In this lesson you'll answer questions like these with a set: a collection that keeps each value once and answers "is it in there?" quickly.

Each value once

Hana and Abel signed the Monday list twice. set() turns a list into a set. Predict how many values the set has:

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

monday = ["Hana", "Abel", "Hana", "Selam", "Abel"]
print(len(set(monday)))
Pick the output

Python prints

3

A set keeps each value once, so the two extra signatures disappear: Hana, Abel, and Selam are left. (2 would mean the repeated names are dropped completely; they're kept once.)

monday = ["Hana", "Abel", "Hana", "Selam", "Abel"]
people = set(monday)
print(len(monday))
print(len(people))
print(sorted(people))

In the editor, press Escape then Tab to move on.

A holds each value at most once. The list had 5 entries; the set has 3 people.

A set also has no order. Printed directly, its items can come out in any order, and the order can differ from run to run. That's why this lesson prints sorted(...), which makes a sorted list: that way you see the same output as the lesson.

You can write a set with curly brackets, and add to it with add(). Adding a value that's already there does nothing:

registered = {"Hana", "Abel"}
registered.add("Hana")
print(len(registered))
registered.add("Meron")
print(len(registered))

In the editor, press Escape then Tab to move on.

An empty set

Curly brackets are also how you write a dict. So what is {}? Predict.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

print(type({}))
print(type(set()))
Pick the output

Python prints

<class 'dict'>
<class 'set'>

Empty curly brackets make an empty dict: dicts came first in Python, so they got {}. An empty set is written set().

The mistake shows up when you try to add to it:

Came to both, came to either

Sets can be combined, and each combination answers a question in plain words:

morning = {"Hana", "Abel", "Selam", "Dawit"}
evening = {"Abel", "Meron", "Dawit"}
print(sorted(morning & evening))  # came to both
print(sorted(morning | evening))  # came to either
print(sorted(morning - evening))  # came only in the morning

In the editor, press Escape then Tab to move on.

  • a & b, the intersection: in both.
  • a | b, the union: in either one (or both).
  • a - b, the difference: in a but not in b.

Now change it to find who came only in the evening. (Order matters for difference: evening - morning isn't the same question.)

Set or list?

Sets are built for one question: "is this value in here?" With a list, in checks the items one by one, so a long list takes longer. A set jumps straight to the answer, almost instantly even with thousands of items.

registered = {"Hana", "Abel", "Selam"}
print("Abel" in registered)
print("Yonas" in registered)

In the editor, press Escape then Tab to move on.

The price is that a set has no positions. There's no first item, so there's no [0]:

Choose a list when order matters or the same value can appear twice (a queue, scores in the order they came). Choose a set when each value should appear once and you mostly ask "is it in there?".

ጥያቄ

A program checks, again and again, whether a student has already registered. Which collection fits best for the registered names?

መልመጃ

Came to both sessions

Two attendance lists, with some names written twice. Print, in alphabetical order and one per line, the students who came to both sessions.

Output
Abel
Dawit
Meron

Your code

morning = ["Hana", "Abel", "Meron", "Dawit", "Abel", "Selam"]
evening = ["Dawit", "Yonas", "Meron", "Abel", "Dawit"]
# Make sets, find who came to both, and print them sorted

In the editor, press Escape then Tab to move on.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

morning = ["Hana", "Abel", "Meron", "Dawit", "Abel", "Selam"]
evening = ["Dawit", "Yonas", "Meron", "Abel", "Dawit"]
both = set(morning) & set(evening)
for name in sorted(both):
    print(name)

ዋና ዋና ነጥቦች

  • A set keeps each value once and has no order: set(names) removes duplicates.
  • {} is an empty dict; an empty set is set(). Add with .add().
  • & is "in both", | is "in either", - is "in the first but not the second".
  • Sets answer in quickly but have no positions, so no [0]. Print sorted(...) to get a stable order.
  • Use a list for order and repeats; use a set for "is it in there?".