1. Hash Table Mechanics & O(1) Lookups
Why Sets Beat Lists for Membership Checks
- Under the hood, Python sets are implemented as open-addressing hash tables.
- To insert an element, Python computes `hash(element)` and maps it to a bucket index in memory.
- Looking up an item `x in my_set` is an average O(1) constant time operation, compared to O(N) linear time for lists.
registered = {"[email protected]", "[email protected]"}
print("Is bob registered?", "[email protected]" in registered) # O(1) instant lookup
💡 Key Takeaway: Always use sets when deduplicating values or performing repeated membership tests.