Beginner · Lists, tuples, dictionaries and sets · topic 17 of 50 · 15 min
Sets: each item only once
Explanation
A set keeps each item only once
A set is a collection of unique items. Unique means each item can appear only once. If you add a duplicate, the set simply ignores it. You write a set with curly braces { }, like a dict, but with single items instead of key: value pairs.
cities = {"Pune", "Delhi", "Pune", "Chennai"}
print(len(cities))
print("Delhi" in cities)Removing duplicates from a list
phones = ["98450", "98111", "98450", "99000", "98111"]
unique = set(phones)
print(len(unique))
print(sorted(unique))Adding and removing
present = {"Asha", "Ravi"}
present.add("Meena")
present.add("Asha")
present.discard("Ravi")
present.discard("John")
print(sorted(present))There is also remove(), which works like discard but gives KeyError when the item is missing. discard is the safer choice when you are not sure.
Why sets are fast
To check x in my_list, Python looks at the items one by one, which is slow for a lakh of items. A set stores items using a method called hashing, which turns each item into a number that says where it is kept. So x in my_set is almost instant, even for millions of items. If you check membership many times, turn the list into a set first.
Set maths: students in two clubs
cricket = {"Asha", "Ravi", "Meena", "Arjun"}
dance = {"Meena", "Priya", "Arjun"}
print(sorted(cricket | dance))
print(sorted(cricket & dance))
print(sorted(cricket - dance))| Operation | Symbol | Method | Gives |
|---|---|---|---|
| union | a | b | a.union(b) | items in either set |
| intersection | a & b | a.intersection(b) | items in both sets |
| difference | a - b | a.difference(b) | items in a but not in b |
a = {}
b = set()
print(type(a))
print(type(b))Watch the lesson
Sets: each item only once · 1/7
In this lesson you will learn sets, collections where each item appears only once.
Prefer a video by a teacher? Find free videos on “python sets tutorial union intersection for beginners” on YouTube ↗
In real life: Finding regular customers
A tea stall owner collects the UPI phone numbers of customers on Monday and on Tuesday. Some people paid twice on the same day. With sets he finds how many different people came, who came on both days (his regulars), and who came only on Tuesday (new faces).
monday = ["98450", "98111", "98450", "99000"]
tuesday = ["98111", "97777", "99000", "97777"]
mon = set(monday)
tue = set(tuesday)
print("Different customers Monday:", len(mon))
print("Came both days:", sorted(mon & tue))
print("New on Tuesday:", sorted(tue - mon))Practice (0/3 done)
Write a function unique_count(items) that returns how many DIFFERENT items are in the list. Example: unique_count(["chai", "coffee", "chai", "tea"]) returns 3.
Write a function in_both(club_a, club_b) that takes two lists of names and returns a SORTED list of the names that are in both clubs.
Write a function new_customers(today, regulars) that returns a SORTED list of the people in the list today who are NOT in the list regulars. Each name should appear only once.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.