Python Set vs List: 10,000x Performance Boost

One character change. 10,000x faster. When you check if something exists in a list: username in allowed_users_list Python checks every element. One by one. That's O(n). When you check if something exists in a set: username in allowed_users_set Python computes a hash and jumps directly to the answer. That's O(1). See the code 👇 With 10,000 users, the set version is roughly 10,000x faster for each lookup. The mental model: → List membership = Reading every name on a guest list → Set membership = Looking up a name in a phone book index In AI applications, this shows up everywhere: → Checking if a document is already in your vector store → Filtering duplicate embeddings → Validating allowed API keys The structure you choose isn't just about "what works." It's about what works at scale. This is adapted from my upcoming book, Zero to AI Engineer: Python Foundations. I share excerpts like this on Substack → https://lnkd.in/eFVTjauz #Python #Programming #DataStructures #AI #Performance

  • text

To view or add a comment, sign in

Explore content categories