TGViewer
Python Learning Python Learning @python_bds · 5.74K subscribers
Post #1281 657
⚡️ Why Is This Loop So Slow?

Imagine you're checking whether thousands of usernames exist.
for username in usernames:
if username in banned_users:
...


If banned_users is a list, Python checks one element at a time.
Alice?

Bob?

Charlie?

David?

...

For every lookup.

Now imagine banned_users is a set. Python doesn't search one by one. It uses a hash table to jump directly to where the value should be.

That's why changing this:
banned_users = [...]

into this:
banned_users = {...}

can dramatically speed up membership checks without changing the rest of your code.
  • ❤ 3
More from @python_bds
  1. Oct 8, 2026🐛 This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  2. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  3. Oct 4, 2026Python Set Methods ✍️
  4. Oct 3, 2026Post #1310
  5. Oct 1, 2026🐍 Python Beginner Notes
  6. Sep 30, 2026🧠 return vs print() in Python These are not interchangeable. def add(a, b): print(a + b)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →