CodeOath
← All posts
Python70 min total · 18 parts

Python Fundamentals for Interviews: Data Structures, Comprehensions, and Gotchas

Contents — Part 18 of 18: Common Mistakes Worth Remembering
Part 18 of 18 · ~1 min

Common Mistakes Worth Remembering

  • A mutable default argument (def f(x=[])) or mutable class attribute — one of Python's most-asked "spot the bug" interview questions.
  • Using == where is None was intended, or vice versa.
  • Modifying a list while iterating over it directly, which silently skips elements — iterate over a copy (for x in list(original):) if the loop body needs to mutate the original.
  • Reaching for a list when a set would make a membership check O(1) instead of O(n).
  • Building a large string with repeated += in a loop instead of str.join, turning an O(n) operation into O(n²).
  • Late-binding closures in a loop ([lambda: i for i in range(3)]) — every lambda sees the loop variable's final value, not the value at the time it was created.
  • A bare except: swallowing KeyboardInterrupt and SystemExit along with the bug you actually meant to catch.
  • Confusing a shallow copy with a deep copy on nested lists/dicts, then being surprised that mutating the "copy" affects the original too.

Practice these exact ideas — including the mutable-default gotcha and the closure-in-a-loop trap — as real "predict the output" questions in the code lab. Python's web-framework side is covered separately in Django Fundamentals.