Lesson 27 of 60
โฎPrev Nextโฏ

PY-101 ยท Collections

Sets

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

27of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    A set is an unordered collection of unique values in curly brackets, such as {"tea", "milk"}. Adding a value that is already there does nothing, so sets are the quickest way to remove duplicates. They also answer "what do these two groups share?" with & (intersection), | (union) and - (difference).

    • Remove duplicates with set()
    • Test membership with in
    • Use &, | and -

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Sets, running
    1. emails = ["a@x.com", "b@x.com", "a@x.com", "c@x.com"]
    2. unique = set(emails)
    3. print(len(emails), "->", len(unique))
    4. team_a = {"Asha", "Ravi", "Li"}
    5. team_b = {"Ravi", "Li", "Sam"}
    6. print(sorted(team_a & team_b))
    7. print(sorted(team_a - team_b))
    8. print("Sam" in team_b)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemTwo teams sent you their attendee lists. Who came to both events? Who came only to one? With sets that is one line each.
    2. The Python ideas = {a, b}A set is an unordered collection of unique values in curly brackets, such as {"tea", "milk"}.
    3. In real lifeMarketing tools use set difference to find customers who signed up but never bought: signed_up - buyers.

    Start with a question

    Two teams sent you their attendee lists. Who came to both events? Who came only to one? With sets that is one line each.

    De-duplicating emails, finding common customers and checking "have I seen this before?" are everyday data jobs.

    Explain it like I'm 10

    A set is a sticker album: each sticker can only be in it once. Get a second copy and it does not go in.

    The technical explanation

    set(["a", "b", "a"]) gives {"a", "b"}. Order is not kept, so you cannot index a set.

    x in my_set is very fast even with millions of items.

    a & b is items in both, a | b items in either, a - b items in a but not in b.

    Syntax

    s = {a, b}
    s.add(x)
    a & b    a | b    a - b

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Marketing tools use set difference to find customers who signed up but never bought: signed_up - buyers.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. len(set([1, 1, 2, 3, 3]))?
    2. 2. How do you make an empty set?
    3. 3. {1, 2} & {2, 3}?
    4. 4. Can you index a set with s[0]?
    5. 5. {1, 2, 3} - {2}?

    Questions people ask

    How do I remove duplicates from a list in Python?

    list(set(items)) removes duplicates but loses the order. To keep order use list(dict.fromkeys(items)).

    Why is checking membership in a set faster than a list?

    Sets use hashing, so Python jumps straight to where a value would be instead of checking every item.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner How do I remove duplicates from a list in Python?

    list(set(items)) removes duplicates but loses the order. To keep order use list(dict.fromkeys(items)).

    beginner Why is checking membership in a set faster than a list?

    Sets use hashing, so Python jumps straight to where a value would be instead of checking every item.

    intermediate len(set([1, 1, 2, 3, 3]))?

    3. Duplicates are dropped: {1, 2, 3}.

    intermediate How do you make an empty set?

    set(). {} is an empty dict.

    Quick revision

    Remember

    • Remove duplicates with set()
    • Test membership with in
    • Use &, | and -
    • {} is an empty dictionary, not an empty set. Write set() for an empty set.

    Syntax

    s = {a, b}
    s.add(x)
    a & b    a | b    a - b

    Official documentation

    • Sets (docs.python.org)