Skip to main content

Command Palette

Search for a command to run...

Unlocking Python Sets: Fast, Clean, and Always Unique

Published
9 min readView as Markdown
Unlocking Python Sets: Fast, Clean, and Always Unique

When working with data in Python, sets play a crucial role in ensuring efficiency, uniqueness, and speed. In this blog, we’ll explore what sets are, why they are useful, and how they are applied in the industry.

What is a Python Set?

A set is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicates. They are defined using curly braces {} or the set() constructor.

Key Characteristics of Sets:

  • Unique Elements Only: Automatically removes duplicates.

  • Unordered: No guarantee on the order of items.

  • Mutable: You can add or remove elements after creation.

  • Unindexed: You can’t access items by index ( my_set[0] will raise an error).

Example:-

my_set = {1, 2, 3, 4, 4, 2}
print(my_set)  # Output: {1, 2, 3, 4}

Notice that even though 4 and 2 appear twice, the set only keeps one copy of each.

Creating Sets:

You can create sets using curly braces {} or the set() constructor:

# Using curly braces
chai = {'green', 'black', 'herbal', 'chai', 'masala', 'ginger', 'lemon'}

# Using set() constructor (useful for empty sets)
empty_set = set()  # NOT {} —> that creates an empty dictionary

Why Use Sets?

  • To remove duplicates from a list.

  • To perform mathematical set operations like union, intersection, and difference.

  • To do fast membership tests (checking if an item is in a collection).

setOne = {1, 2, 3, 4, 5}
setTwo = {4, 5, 6, 7, 8, 9}

# Length of a set
print("Length of Set One:", len(setOne)) # Output: 5
print("Length of Set Two:", len(setTwo)) # Output: 6

# Union of two sets
print("Union:", setOne | setTwo) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9}

# Intersection of two sets
print("Intersection:", setOne & setTwo) # Output: {4, 5}

# Difference between two sets
print("Difference (SetOne - SetTwo):", setOne - setTwo) # Output: {1, 2, 3}
print("Difference (SetTwo - SetOne):", setTwo - setOne) # Output: {8, 9, 6, 7}

# Symmetric difference between two sets
print("Symmetric Difference:", setOne ^ setTwo) # Output: {1, 2, 3, 6, 7, 8, 9}

# Check if an element is in a set
print("Is 1 in Set One:", 1 in setOne) # Output: True
print("Is 1 in Set Two:", 1 in setTwo) # Output: False

Why Sets Are Fast: The Hashing Advantage

One of the biggest strengths of Python sets is speed, especially when checking if an item exists in the set. But what makes them so fast?

The answer lies in hashing — a powerful mechanism behind the scenes.

How Hashing Works

When you add an item to a set:

  1. Applies a hash function to the item to convert it into a fixed-size number (called a hash value).

  2. Stores the item in a hash table (a special kind of data structure).

  3. To check if an item exists, Python just calculates its hash and jumps directly to its position, no need to search through every element.

This means membership tests (in) are done in constant time, O(1), on average.

Example: List vs Set Lookup Time

# List (O(n) lookup time)
nums_list = list(range(1000000))
print(999999 in nums_list)  # Slower

# Set (O(1) average lookup time)
nums_set = set(range(1000000))
print(999999 in nums_set)  # Much faster

List → Python checks each item one by one until it finds a match, which could take a long time.
Set → Python just jumps to the right spot, thanks to hashing.

  • Sets use hash tables, which allow instant lookups.

  • This makes them much faster than lists for checking if an item exists.

Important Notes About Hashing in Sets

  • Only hashable (immutable) items can be added to a set.
    That’s why sets can contain numbers, strings, and tuples, but not lists or dictionaries.
my_set = {1, "hello", (2, 3)}  # 
my_set = {[1, 2]}              # TypeError: unhashable type: 'list'
  • The order of elements is not preserved. Sets care more about hashing than order.

Common Use Cases for Python Sets

Python sets are not just fast, they are practical. Whether you are cleaning data, comparing lists, or improving performance, sets offer powerful tools for real-world problems.

Here are some of the most common and effective use cases:

1. Removing Duplicates from a List

Need only the unique values from a list? Use a set.

tea_varities = ['green', 'black', 'chai', 'green', 'ginger', 'black', 'masala', 'ginger', 'lemon']
unique_names = set(tea_varities)
print(unique_names)  

# {'lemon', 'ginger', 'green', 'chai', 'black', 'masala'}

Use case: Cleaning messy datasets or ensuring uniqueness.

2. Fast Membership Testing

When you need to check if a value exists.

check_varities = ['green', 'black', 'chai', 'green', 'ginger', 'black', 'masala', 'ginger', 'lemon']
print("chai" in check_varities)   # True
print("coffee" in check_varities)  # False

Use case: Login systems, filtering inputs, and access control.

3. Set Operations: Union, Intersection, Difference

Python sets support math-like operations, great for comparing data.

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)   # Union: {1, 2, 3, 4, 5}
print(a & b)   # Intersection: {3}
print(a - b)   # Difference: {1, 2}
print(a ^ b)   # Symmetric Difference: {1, 2, 4, 5}

Use case: Finding shared users, different elements, merging data.


4. Filtering Data Efficiently

You can use sets to filter or deduplicate values from one collection based on another.

visited_pages = {"home", "about", "contact"}
all_pages = ["home", "products", "about", "blog"]

# Find new (unvisited) pages
new_pages = [page for page in all_pages if page not in visited_pages]
print(new_pages)  # Output: ['products', 'blog']

Use case: Web crawlers, data pipelines, recommendations.


5. Finding Duplicates

Sets can help identify duplicates, too, just track what you've seen.

items = [1, 2, 3, 2, 4, 3, 5]
seen = set()
duplicates = set()

for item in items:
    if item in seen:
        duplicates.add(item)
    else:
        seen.add(item)

print(duplicates)  # Output: {2, 3}

Use case: Data validation, audit logs, fraud detection.

6. Set Comprehensions

list comprehensions, but for sets.

squares = {x*x for x in range(5)}
print(squares)  # Output: {0, 1, 4, 9, 16}

Use case: When you need a unique, computed collection.


How Are Sets Used in the Industry?

In the real world, Python sets are not just theoretical tools, they are widely used across tech companies, data science teams, cybersecurity, and backend systems for solving practical problems.

Here’s how sets power real-world applications across industries:

1. Web Development & APIs

Use Case: Filtering and Access Control

Web backends use sets to:

  • Track user roles and permissions

  • Filter out duplicate requests or IPs

  • Validate inputs efficiently

allowed_roles = {"admin", "editor", "moderator"}
if user_role in allowed_roles:
    grant_access()

Why sets? Instant lookup for access validation (O(1) time).

2. Cybersecurity

Use Case: IP Blacklisting and Threat Detection

Security systems use sets to:

  • Store blacklisted IP addresses.

  • Check known malicious URLs

  • Track previously seen attack signatures.

blocked_ips = {"192.168.1.100", "10.0.0.5"}
if request_ip in blocked_ips:
    block_request()

Why sets? Fast and efficient lookup in massive datasets.

3. Data Science & Analytics

Use Case: Removing Duplicates and Comparing Datasets

Analysts and data engineers use sets to:

  • Clean raw data (remove duplicates)

  • Compare large datasets (using intersection, difference)

  • Detect anomalies

sales_2023 = {"Alice", "Bob", "Charlie"}
sales_2024 = {"Bob", "Diana"}

repeat_customers = sales_2023 & sales_2024  # Intersection

Why sets? Quick comparison and de-duplication of large data collections.

4. E-commerce & Recommendation Engines

Use Case: User Behavior & Item Matching

E-commerce platforms use sets to:

  • Track unique user actions (clicks, views)

  • Recommend items that are similar but not yet seen.

  • Detect overlap in preferences between users.

user1_likes = {"shoes", "jeans", "jackets"}
user2_likes = {"jeans", "hats", "sneakers"}

similar_items = user1_likes & user2_likes

Why sets? Great for collaborative filtering and user-item graph analysis.

5. Email and Communication Systems

Use Case: Spam Detection and Contact Management

Email services use sets to:

  • Detect duplicate emails or spam.

  • Manage contact groups without duplication.

  • Track flagged phrases or blacklisted domains.

spam_keywords = {"win", "free", "urgent", "money"}
if any(word in spam_keywords for word in email_words):
    flag_as_spam()

Why sets? Efficient keyword matching in incoming content.

6. Supply Chain and Inventory Systems

Use Case: Unique Item Tracking

Logistics systems use sets to:

  • Track unique items in a warehouse.

  • Prevent duplicate scans of shipments.

  • Compare available vs. required parts

scanned_items = set()
if item_id not in scanned_items:
    scanned_items.add(item_id)

Why sets? Real-time uniqueness tracking at scale.

Used in the Industry

IndustryApplicationWhy Sets?
Web DevUser roles, filtering requestsFast lookups, clean logic
CybersecurityIP blacklists, threat detectionSpeed, memory efficiency
Data ScienceData cleaning, comparisonsDe-duplication, easy ops
E-commerceRecommendation systemsSimilarly, overlap checks
Email SystemsSpam detection, contact filteringKeyword matching
LogisticsInventory, scanning systemsUnique tracking

When Not to Use Sets in Python

While Python sets are powerful, with fast lookups, automatic uniqueness, and handy operations, they’re not always the right choice. There are specific situations where using a set could lead to bugs, inefficiencies, or unexpected behaviour.

Let’s look at when not to use sets:

1. When You Need to Preserve Order

Sets do not maintain insertion order (Python 3.7). Even in newer versions, relying on order from a set is not recommended for logic.

Use instead: list or collections.OrderedDict (if order matters).

my_list = ["a", "b", "c"]
print(my_list[0])  # 'a'

my_set = {"a", "b", "c"}
# print(my_set[0])  # Error: sets are unordered and unindexed

2. When You Need Duplicates

Sets eliminate duplicate values automatically.

Use instead: list or collections. Counter if you need to track frequencies.

items = ["apple", "apple", "banana"]
my_set = set(items)
print(my_set)  # Output: {'apple', 'banana'} — one "apple" is removed!

3. When You Need Index-Based Access

Sets do not support indexing (my_set[0] is invalid).

Use instead: list or tuple.

4. When You Need to Store Unhashable (Mutable) Types

Sets can only contain hashable (immutable) objects. Lists, dictionaries, and other sets (mutable types) cannot be added.

my_set = set()
# my_set.add([1, 2, 3])  # TypeError: unhashable type: 'list'

Use instead: Consider using tuples (immutable) or redesigning the data structure.


Summary: Don't Use Sets When...

SituationBetter Alternative
You need to maintain orderlist, OrderedDict
You need duplicateslist, Counter
You need index-based accesslist, tuple
You want to store unhashable typeslist, dict
You're working with tiny datalist
You're optimizing for low memorylist, array

Resource :- https://github.com/ajit421/python/tree/main/number/set

linkedin:- https://www.linkedin.com/in/ajit7900