KwickAcademy Python · 7 min · free
Sets and Set Operations
Learn Python sets: why duplicates disappear, union, intersection, difference, set methods, and when to use a set. A set is a group of unique items inside curly brackets, with no fixed order and no index.
On screen in this lesson
What is a set?
| A set is a group of unique items in { } |
| Duplicates are removed automatically |
| No fixed order, so no indexing |
| Items must be immutable: numbers, strings, tuples |
Why duplicates disappear
| Python makes a hash code from each value |
| The hash code decides where the item is stored |
| Equal values get the same hash code |
| So a second copy finds its place already taken |
The four set operations
| Union a | b : items in either set |
| Intersection a & b : items in both sets | |
| Difference a - b : in a but not in b | |
| Symmetric difference a ^ b : in one set only |
Set vs list
| Feature | List | Set |
|---|---|---|
| Brackets | [ ] | { } |
| Duplicates | Allowed | Removed |
| Order and index | Yes | No |
| Checking with in | Slower | Very fast |
Set or list?
| Set: remove duplicates |
| Set: fast in checks, like a list of valid coupon codes |
| Set: common or different items between groups |
| List: order matters, repeats matter, or you need indexes |
Quick recap
| A set holds unique items in { }, with no index | |
| Empty set is set(), because {} is a dictionary | |
| union, & intersection, - difference, ^ symmetric | |
| add, update, remove, discard, pop, clear | |
| Use a set for unique items and fast in checks |
Quick answers
How do you make an empty set?
Write set(). Empty curly brackets {} make a dictionary.
What is the difference between remove() and discard()?
For a missing item, remove() gives a KeyError, but discard() does nothing.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why is the length of {3, 1, 3, 2, 1} three?42 sec
Which operator finds items in both sets?46 sec
Which set method can crash on a missing item?43 sec
Why use a set instead of a list?43 secThe full lesson, in text
Hello students, welcome to Kwickprep. Your attendance list has Riya written twice by mistake. How do you keep just one of each name, without writing a loop? Python has a container that does it for you, called a set. Today we will learn sets, the four set operations, set methods, and when a set beats a list.
A set is like the idea of a set in maths. It is a group of unique items, written inside curly brackets. Unique means no item appears twice, so duplicates are removed automatically. A set has no fixed order, so you cannot pick an item by index. Its items must be immutable, which means values that cannot change, like numbers, strings or tuples.
Here we write five roll numbers, but three and one appear twice. The set keeps only one copy of each, so len gives three. The set function turns a list into a set. The names list has three entries, but only two unique names. A set has no fixed order, so never rely on the order in which it prints.
Why does this happen? Python turns each value into a number called a hash code. That number decides where the item is stored inside the set, like a seat number. Equal values always get the same hash code. So when a second copy arrives, its seat is already taken, and it is not added again. This is also why a list, which can change, cannot go inside a set.
Pause and predict. Is box, written with empty curly brackets, an empty set? No. Empty curly brackets make an empty dictionary. To make an empty set, you must write set with empty round brackets. Also, rolls at zero would give a type error, because a set has no index.
Sets are famous for four operations, just like Venn diagrams in maths. Union uses the vertical bar operator, and gives items in either set. Intersection uses the ampersand operator, and gives items in both sets. Difference uses the minus operator, and gives items in the first set but not the second. Symmetric difference uses the caret operator, and gives items in exactly one of the two sets.
These are roll numbers of students in the cricket club and the chess club. The union gives every student in at least one club, and two and six are not repeated. The intersection gives students who are in both clubs, roll numbers two and six. You can also write cricket dot union of chess, and cricket dot intersection of chess.
Cricket minus chess gives students only in cricket, one and four. Order matters here, so chess minus cricket gives only five. Symmetric difference gives students in exactly one club, one, four and five. The method names are difference and symmetric difference, with an underscore between the two words.
Now the set methods. Add puts one item in the set, and adding two again changes nothing. Remove and discard both delete an item. The difference is what happens when the item is missing. Discard of nine quietly does nothing. But remove of a missing item gives a key error.
Update adds many items at once, from a list or another set. Two is already there, so only three and four are added. The in operator checks membership, and four is in seats. Pop removes and returns some item, but you cannot choose which; here it gives one. Clear empties the set, and an empty set prints as set with round brackets.
Three methods compare whole sets. Issubset asks, is every item of team also in club? Yes. Issuperset asks the reverse, does club contain all of team? Yes. Isdisjoint asks, do the two sets have nothing in common? Team and the set five, seven share nothing, so it is True.
Let us compare a set with a list. A list uses square brackets, and a set uses curly brackets. A list allows duplicates, but a set removes them. A list keeps order and has indexes, but a set has neither. Checking with in is slower on a long list, because Python looks item by item, while a set jumps straight to the hash code.
Here is a real use. A school library records every visit, so Riya appears twice. The list has four visits, but the set has three unique visitors. A set has no order, so we use sorted to print the names alphabetically as a list.
So when should you choose a set? Use a set to remove duplicates. Use a set when you check membership again and again, like valid coupon codes. Use a set to find common or different items between two groups. But use a list when order matters, when repeats matter, like marks in every test, or when you need indexes.
Let us revise what we learned today. A set holds unique items in curly brackets, and it has no index. An empty set is set with round brackets, because empty curly brackets make a dictionary. The four operations are union, intersection, difference and symmetric difference. The main methods are add, update, remove, discard, pop and clear. Use a set when you need unique items or fast membership checks.
Courses that teach this
| Course | Unit |
|---|---|
| Programming All levels Python | Data Structures |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

