Time complexity of python set operations?

Python sets are powerful data structures that resemble mathematical sets. They are implemented using hash tables, similar to dictionaries in Python but only storing keys without associated values. Due to their hash table implementation, most of the common operations on sets have efficient time complexities. Here is an overview of the time complexities for various operations on Python sets:

Average Case Time Complexities

  • Insertion (Add): O(1)

    • Adding an element to a set is generally an O(1) operation. The element is hashed, and then placed in a bucket based on its hash value.
  • Deletion (Remove, Discard): O(1)

    • Removing an element, like adding, involves computing its hash and locating it in its bucket, making it an O(1) operation on average.
  • Lookup (Check for membership): O(1)

    • Checking whether an element is in a set is O(1) because it hashes the element and checks the corresponding bucket.
  • Length (Size of the set): O(1)

    • Python stores the number of elements in a set as an instance variable, making the length retrieval operation O(1).

Set Operations

  • Union (s | t): O(len(s) + len(t))

    • To form a union, Python iterates through all elements in both sets. The time complexity is linear with respect to the total number of elements in both sets.
  • Intersection (s & t): O(min(len(s), len(t)))

    • Python iterates over the smaller set and checks for the existence of each element in the larger set. This approach optimizes the operation, as fewer membership tests are needed when the smaller set is used.
  • Difference (s - t): O(len(s))

    • The difference requires a check for each element in the first set to see if it is not in the second set, which leads to a time complexity proportional to the size of the first set.
  • Symmetric Difference (s ^ t): O(len(s) + len(t))

    • Symmetric difference is implemented as a combination of differences and unions, so its time complexity is linear with respect to the total number of elements in both sets.

Special Operations

  • Subset Check (s <= t): O(len(s))
    • Checking if set s is a subset of set t involves checking every element in s for membership in t.

Worst Case Considerations

While these operations are O(1) on average, the worst-case time complexity can degrade to O(n) for operations like insertion, deletion, and lookup, particularly in scenarios with high collision rates in the hashing process. However, Python's implementation of sets uses techniques to minimize collisions and maintain efficient performance.

In practice, due to Python's efficient handling of collisions and dynamic resizing of set structures, these O(n) worst-case scenarios are rare. Python sets are highly optimized for rapid membership checking and are one of the most performance-efficient options for this use case in Python's standard data structures.

Conclusion

Python sets offer efficient average-case performance for a variety of operations due to their hash table-based implementation. Understanding these complexities can help you make informed decisions about when and how to use sets in Python to optimize the performance of your applications.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Where do you go for inspiration?
Who is the CEO of Cisco?
Which skill is best for software engineering?
Which platform is best for interview preparation?
Why tech interview questions?
What is an aptitude test in software engineering?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.