Sets in Python

When working with data, sometimes we need to store unique values only. For example, if you are collecting email IDs from a form and don’t want duplicates, then Sets in Python are the perfect choice.

sets in python

Let’s explore what Sets are, how they work, their useful functions, and real-life examples.


What is a Set in Python?

A Set is a collection of unordered, unindexed, and unique items.

  • No duplicate values allowed
  • Items can be of different data types (int, str, float, etc.)
  • They are enclosed in { } curly brackets

Example:

fruits = {"apple", "banana", "mango", "apple"}  
print(fruits)  

Output:

{'banana', 'apple', 'mango'}

👉 Notice how "apple" is written twice, but stored only once.


Creating Sets

# Empty set
myset = set()

# Set with values
numbers = {1, 2, 3, 4, 5}

# Mixed data
mixed = {1, "hello", 3.14, True}

Also Read: Tuples in Python


Useful Set Methods & Functions

Python provides many built-in methods for sets. Let’s explore them with examples.

1. add() – Add a single element

fruits = {"apple", "banana"}
fruits.add("mango")
print(fruits)

2. update() – Add multiple elements

fruits = {"apple", "banana"}
fruits.update(["grapes", "orange"])
print(fruits)

3. remove() / discard() – Remove an element

fruits = {"apple", "banana", "mango"}
fruits.remove("banana")   # error if not present
fruits.discard("grapes")  # no error if not present

4. pop() – Removes a random element

fruits = {"apple", "banana", "mango"}
fruits.pop()
print(fruits)

5. clear() – Empty the set

fruits = {"apple", "banana"}
fruits.clear()
print(fruits)  # Output: set()

6. union() – Combine sets (like OR)

set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))

Output:

{1, 2, 3, 4, 5}

7. intersection() – Common values (like AND)

set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.intersection(set2))

Output:

{2, 3}

8. difference() – Values only in first set

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
print(set1.difference(set2))

Output:

{1, 2}

9. symmetric_difference() – Values not common in both

set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.symmetric_difference(set2))

Output:

{1, 2, 4, 5}

Read More: Lists in Python


Real-Life Examples of Sets

Example 1: Removing duplicate email IDs

emails = ["a@gmail.com", "b@gmail.com", "a@gmail.com", "c@gmail.com"]
unique_emails = set(emails)
print(unique_emails)  
# Output: {'a@gmail.com', 'c@gmail.com', 'b@gmail.com'}

Example 2: Finding common students in two courses

python_course = {"Amit", "Neha", "Ravi"}
java_course = {"Ravi", "Neha", "Sonia"}

common = python_course.intersection(java_course)
print("Students in both courses:", common)
# Output: Students in both courses: {'Neha', 'Ravi'}

Example 3: Detecting unique website visitors

visitors = ["101", "102", "101", "103", "102", "104"]
unique_visitors = set(visitors)
print(f"Total unique visitors: {len(unique_visitors)}")
# Output: Total unique visitors: 4

Final Thoughts

Sets in Python are very useful when:

  • You want to store unique items
  • You need to perform mathematical set operations (union, intersection, difference)
  • You want to quickly remove duplicates

They might not preserve order, but they are fast and powerful when it comes to handling unique collections.

FAQs – Sets in Python

  • Lists allow duplicates and maintain order.

  • Sets do not allow duplicates and are unordered.

You must use set() because {} creates an empty dictionary.

myset = set()

  • When you want to store only unique values

  • When performing mathematical operations like union, intersection, or difference

  • When you want faster membership checking (using in)

Because Python uses a hashing mechanism to store set elements, which makes them unordered but ensures uniqueness and fast lookup.

What’s Next?

In the next post, we’ll learn about the Dictionaries in Python

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »
Scroll to Top