Sobes.tech
Junior — Middle

How to determine the time complexity of reading elements in sets implemented in Python?

sobes.tech AI

Answer from AI

In Python, sets (set) are implemented based on hash tables.

The read operation (checking for the presence of an element) in a set has an amortized time complexity of O(1) — meaning it executes in constant time on average.

This means that checking whether an element is in a set is very fast and does not depend on the size of the set.

Example:

s = {1, 2, 3, 4}
if 3 in s:
    print("Element found")

Here, the operation 3 in s is performed in O(1) on average.