Python Sets

What We Cover in This Tutorial

We will go through everything you need to know about Python sets, from zero to hero. Here’s the complete list:

  • What is a Set and why it is useful
  • How to create a Set
  • Important properties of Sets
  • Adding elements to a Set
  • Removing elements from a Set
  • Checking if an item exists
  • Looping through a Set
  • All built-in Set methods (with examples)
  • Mathematical Set Operations (Union, Intersection, Difference, etc.)
  • Set Comprehensions (a shortcut way to create sets)
  • Frozen Sets (the “read-only” version of sets)
  • Real-world examples and common mistakes
  • Practice exercises with solutions

Table of Contents

  1. What is a Python Set?
  2. Creating a Set
  3. Properties of a Set
  4. Adding Elements to a Set
  5. Removing Elements from a Set
  6. Checking Membership (Is an item in the set?)
  7. Iterating (Looping) Through a Set
  8. Built-in Set Methods
  9. Set Operations (Mathematics)
  10. Set Comprehensions
  11. Frozen Sets
  12. Real-World Examples
  13. Common Mistakes & Tips
  14. Practice Exercises

1. What is a Python Set?

A Set in Python is a collection of items where:

  • Every item is unique (no duplicates allowed)
  • The order of items does not matter (unordered)
  • You can add or remove items later (it is mutable)

Real-life analogy:
Think of a set like a bag of marbles where:

  • You cannot put two identical marbles (duplicates are automatically removed)
  • You don’t care which marble comes out first
  • You can add or remove marbles anytime

Why use Sets?

  • Automatically remove duplicates
  • Very fast to check if something exists (much faster than lists)
  • Perfect for finding common or different items between groups

2. Creating a Set

There are two easy ways to create a set:

Method 1: Using curly braces {}

# Creating a set of fruits
fruits = {"apple", "banana", "cherry"}
print(fruits)

Method 2: Using the set() function

numbers = set([1, 2, 3, 4])          # from a list
letters = set("hello")               # from a string
empty_set = set()                    # empty set (IMPORTANT: {} creates a dictionary!)

Output example:

{'apple', 'banana', 'cherry'}

Pro Tip:
{} creates an empty dictionary, not a set. Always use set() for an empty set.


3. Properties of a Set

PropertyDescriptionExample
UnorderedNo guaranteed orderItems can print in any order
Unique elementsDuplicates are automatically removed{"a", "a", "b"}{"a", "b"}
MutableYou can add/remove items after creationYes
Elements must be immutableOnly numbers, strings, tuples allowed (not lists){"apple", 42, (1,2)} works
Cannot be indexedNo set[0] because order doesn’t existError if you try

4. Adding Elements to a Set

add() – Add one item

colors = {"red", "blue"}
colors.add("green")
print(colors)        # {'red', 'blue', 'green'}

update() – Add multiple items (from list, tuple, or another set)

colors.update(["yellow", "purple", "red"])   # "red" already exists → ignored
print(colors)

5. Removing Elements from a Set

MethodWhat it doesError if item not found?
remove()Removes itemYes (raises error)
discard()Removes item (safer)No
pop()Removes and returns one random itemNo (works on empty too)
clear()Removes all itemsNo

Examples:

fruits = {"apple", "banana", "cherry"}

fruits.remove("banana")      # works
# fruits.remove("mango")     # ← would give KeyError!

fruits.discard("mango")      # safe, does nothing

item = fruits.pop()          # removes one random item
print("Removed:", item)

fruits.clear()               # now empty

6. Checking Membership (Is an item in the set?)

Use the in keyword — super fast!

fruits = {"apple", "banana", "cherry"}

print("apple" in fruits)     # True
print("mango" in fruits)     # False

7. Iterating (Looping) Through a Set

fruits = {"apple", "banana", "cherry"}

for fruit in fruits:
    print(fruit)

Note: Order may be different every time you run it — that’s normal for sets!


8. Built-in Set Methods

Here’s a quick reference table of all important set methods:

MethodDescriptionExample
add()Add one items.add(5)
update()Add multiple itemss.update([6,7])
remove()Remove item (error if missing)s.remove(5)
discard()Remove item (no error)s.discard(99)
pop()Remove & return random itemx = s.pop()
clear()Remove all itemss.clear()
copy()Make a copynew = s.copy()
len()Count itemslen(s)

9. Set Operations (Mathematics)

Sets shine when you combine them! Here are the 4 most useful operations:

OperationSymbolMethodMeaningExample Result
Union (combine)|union()All items from both setsA ∪ B
Intersection&intersection()Common items onlyA ∩ B
Difference-difference()Items in A but not in BA – B
Symmetric Difference^symmetric_difference()Items in either but not bothA △ B

Code Example:

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B)      # Union: {1,2,3,4,5,6}
print(A & B)      # Intersection: {3,4}
print(A - B)      # Difference: {1,2}
print(A ^ B)      # Symmetric difference: {1,2,5,6}

You can also use the full method names:

print(A.union(B))
print(A.intersection(B))

Union

Gemini_Generated_Image_en29leen29leen29

Intersection

Gemini_Generated_Image_s9lrn5s9lrn5s9lr

Difference

Gemini_Generated_Image_3nkawq3nkawq3nka

Symmetric difference

Gemini_Generated_Image_y2wubdy2wubdy2wu

10. Set Comprehensions

A short and beautiful way to create sets (just like list comprehensions):

# Create a set of squares
squares = {x**2 for x in range(1, 6)}
print(squares)          # {1, 4, 9, 16, 25}

# Set of even numbers from a list
numbers = [1, 2, 3, 4, 5, 6]
evens = {x for x in numbers if x % 2 == 0}
print(evens)            # {2, 4, 6}

11. Frozen Sets

A frozen set is like a set that cannot be changed (immutable).

frozen = frozenset(["apple", "banana"])
# frozen.add("cherry")   ← This will give an error!

When to use?
When you want to use a set as a key in a dictionary or inside another set.


12. Real-World Examples

Example 1: Remove duplicates from a list

names = ["Alice", "Bob", "Alice", "Charlie"]
unique_names = set(names)
print(unique_names)      # {'Alice', 'Bob', 'Charlie'}

Example 2: Find common friends

my_friends = {"Alice", "Bob", "David"}
your_friends = {"Bob", "Charlie", "David"}

common = my_friends & your_friends
print("Common friends:", common)

13. Common Mistakes & Tips

  • Mistake: Trying set[0] → Sets have no index!
  • Tip: Use set() for empty set, not {}
  • Tip: Sets are fastest for membership testing (in)
  • Tip: You cannot put a list or another set inside a set (they are mutable)

14. Practice Exercises

Exercise 1: Create a set of your favorite movies. Add 2 more, then remove one.

Exercise 2: Given two sets A = {10, 20, 30, 40} and B = {30, 40, 50, 60}, print:

  • Union
  • Intersection
  • Items only in A

Exercise 3: Write a set comprehension to create a set of all odd numbers from 1 to 20.

Solutions (try first, then check):

# Exercise 2 solution
A = {10, 20, 30, 40}
B = {30, 40, 50, 60}
print(A | B)
print(A & B)
print(A - B)

# Exercise 3 solution
odds = {x for x in range(1, 21) if x % 2 == 1}
print(odds)

Scroll to Top