What is Set in Python? Operations & Examples Explained
π’ Python Sets Explained
Complete Guide
Unique · Unordered · Union · Intersection · frozenset · Real-Life
A set in Python is an unordered collection that stores only unique values. Sets are written using curly braces { }. If you add duplicate values to a set, Python automatically removes them. Sets are used when you only want to store distinct items and perform mathematical set operations like union, intersection, and difference.
fruits = {"apple", "banana", "mango"} numbers = {1, 2, 3, 2, 1} # duplicates removed print(fruits) print(numbers) print(type(fruits))
{1, 2, 3}
<class 'set'>
| Property | Description |
|---|---|
| Unique | Duplicates are automatically removed |
| Unordered | Items have no guaranteed order |
| Mutable | Can add/remove elements |
| No index | Cannot use set[0] — no position access |
| Hashable items | Items must be immutable (no lists inside) |
| Fast lookup | Checking membership is very fast |
s = {1, 2, 2, 3, 3, 3, 4} print(s) # {1, 2, 3, 4} — duplicates gone!
# Method 1 — curly braces s1 = {1, 2, 3, 4} # Method 2 — set() constructor s2 = set([1, 2, 3, 2]) # from list s3 = set("Python") # from string print(s1) # {1, 2, 3, 4} print(s2) # {1, 2, 3} print(s3) # {'P','y','t','h','o','n'}
set() — not {}, because {} creates an empty dictionary.This is one of the most common mistakes beginners make. In Python, { } creates an empty dictionary, NOT an empty set. To create an empty set you must use the set() constructor.
# ❌ This creates a dict, NOT a set! x = {} print(type(x)) # <class 'dict'> # ✅ This creates an empty set y = set() print(type(y)) # <class 'set'> # Add items to the empty set y.add(1) y.add(2) print(y) # {1, 2}
set() — Empty dict = {}Use add() to add a single item to a set. Use update() to add multiple items at once from a list, tuple, or another set. If you try to add a duplicate, it is simply ignored — no error is raised.
▶ add() — single item:s = {1, 2, 3} s.add(4) s.add(2) # duplicate — ignored print(s) # {1, 2, 3, 4}
s = {1, 2} s.update([3, 4, 5]) print(s) # {1, 2, 3, 4, 5} # Update from another set s.update({6, 7}) print(s) # {1, 2, 3, 4, 5, 6, 7}
Python provides three methods to remove items — remove() removes a specific value (raises error if not found), discard() removes a value safely (no error if not found), and pop() removes and returns a random item.
s = {1, 2, 3, 4, 5} # remove() — raises KeyError if not found s.remove(3) print(s) # {1, 2, 4, 5} # discard() — safe, no error s.discard(10) # 10 not in set — no error print(s) # {1, 2, 4, 5} # pop() — removes random item removed = s.pop() print("Removed:", removed)
Python sets support four powerful mathematical operations. These are similar to Venn diagrams in mathematics and are very useful for comparing and combining data collections.
A = {1, 2, 3, 4} B = {3, 4, 5, 6}
print(A | B) # {1,2,3,4,5,6} print(A.union(B)) # same result
print(A & B) # {3, 4} print(A.intersection(B)) # same result
print(A - B) # {1, 2} print(A.difference(B)) # same result
print(A ^ B) # {1,2,5,6} print(A.symmetric_difference(B)) # same
A set A is a subset of set B if all elements of A are also in B. Set B is then a superset of A. Python provides issubset() and issuperset() methods, or you can use <= and >= operators.
A = {1, 2} B = {1, 2, 3, 4} C = {5, 6} # Is A a subset of B? print(A.issubset(B)) # True print(A <= B) # True # Is B a superset of A? print(B.issuperset(A)) # True print(B >= A) # True # Are they disjoint (no common items)? print(A.isdisjoint(C)) # True
A frozenset is an immutable version of a set. Once created, you cannot add, remove, or change its elements. Because it is immutable and hashable, a frozenset can be used as a dictionary key or as an element inside another set — something a regular set cannot do.
fs = frozenset([1, 2, 3]) print(fs) # frozenset({1, 2, 3}) print(type(fs)) # <class 'frozenset'> # Can be used as dict key d = {frozenset({1,2}): "pair"} # Cannot modify — AttributeError! # fs.add(4) ← error # Set operations still work fs2 = frozenset([2, 3, 4]) print(fs & fs2) # frozenset({2, 3})
You can loop through a set using a for loop just like a list or tuple. However, because sets are unordered, the output order may vary each time you run the program. You cannot access set items by index.
colors = {"red", "green", "blue"} for color in colors: print(color)
green
blue
nums = {1, 2, 3, 4, 5} # Print only even numbers for n in nums: if n % 2 == 0: print(n) # 2, 4
s[0] causes a TypeError.| Method | Description |
|---|---|
| add() | Add one item |
| update() | Add multiple items |
| remove() | Remove item (error if not found) |
| discard() | Remove item safely (no error) |
| pop() | Remove and return random item |
| clear() | Remove all items |
| copy() | Make a copy of the set |
| union() | All items from both sets |
| intersection() | Common items only |
| difference() | Items in A not in B |
| symmetric_difference() | Items not in both |
| issubset() | Check if subset |
| issuperset() | Check if superset |
| isdisjoint() | Check if no common items |
One of the most practical uses of sets is removing duplicates from a list. Since sets only keep unique values, converting a list to a set and back to a list instantly removes all duplicates. This is the simplest and fastest way to deduplicate data in Python.
nums = [1, 2, 2, 3, 4, 3, 5, 1] unique = list(set(nums)) print(unique) # [1, 2, 3, 4, 5] # Remove duplicates from names list names = ["Ali", "Sara", "Ali", "John", "Sara"] unique_names = list(set(names)) print(unique_names)
['Ali', 'Sara', 'John']
sorted() if order matters: sorted(set(nums))Here is a real-world example — a duplicate email filter. When users sign up, emails may be submitted multiple times. Using a set, we can instantly find unique emails, shared emails between two lists, and emails that are only in one list.
list1 = ["ali@mail.com", "sara@mail.com", "john@mail.com", "ali@mail.com"] list2 = ["sara@mail.com", "maya@mail.com", "john@mail.com"] # Remove duplicates from list1 unique = set(list1) print("Unique emails:", len(unique)) # Common in both lists common = set(list1) & set(list2) print("In both lists:", common) # Only in list1 only_l1 = set(list1) - set(list2) print("Only in list1:", only_l1) # All unique emails from both all_unique = set(list1) | set(list2) print("All unique:", all_unique)
In both lists: {'sara@mail.com', 'john@mail.com'}
Only in list1: {'ali@mail.com'}
All unique: {'ali@mail.com', 'sara@mail.com', 'john@mail.com', 'maya@mail.com'}
x in set is O(1)set[0]set[1:3] is not possibles = {} print(type(s)) # dict — not set! # Fix: s = set()
s = {1, 2, 3} print(s[0]) # TypeError — no indexing! # Fix: convert to list first print(list(s)[0])
s = {1, 2} s.add([3, 4]) # TypeError! # Fix: add tuple instead s.add((3, 4)) # ✅
s = {1, 2, 3} s.remove(99) # KeyError! # Fix: use discard() s.discard(99) # ✅ no error
Comments
Post a Comment