Lists
List Operations
Concatenation, repetition, membership
Interview: Tests understanding of Python operator semantics and lexicographic comparison
Python lists support a rich set of operators: concatenation (+), repetition (), membership (in), comparison (<, ==), and integration with built-in functions like sorted(), reversed(), and zip().
Operators
- Concatenation (+):
[1,2] + [3,4]→ [1,2,3,4]. Creates a new list — O(n+m) - In-place concatenation (+=):
lst += [3,4]— modifies in place, same as extend() - Repetition ():
[0] * 5→ [0,0,0,0,0]. Creates references, not copies! - Membership (in):
3 in [1,2,3]→ True. O(n) linear scan - Comparison: Lexicographic (element-by-element):
[1,2] < [1,3]→ True
Built-in Functions with Lists
- sorted(): Returns new sorted list — O(n log n). Supports key function
- reversed(): Returns iterator (not list) — wrap with list() if needed
- zip(): Combine multiple lists element-wise into tuples
- min/max/sum: Work directly on lists of comparable elements
- any/all: Test boolean conditions across lists
- map/filter: Functional alternatives to comprehensions
Performance Note
+ creates a new list each time. Building a list with repeated lst = lst + [item] is O(n^2). Use lst.append(item) or lst += [item] (which calls extend) instead.
Use Cases
Merging and combining data from multiple sources
Custom sorting with key functions
Boolean validation with any() and all()
Parallel iteration with zip() for data processing
Common Mistakes
Using lst = lst + [item] in a loop (O(n^2)) — use append() instead
Forgetting that [mutable_obj] * n creates n references to the same object
Expecting == to compare by identity — it compares by value (use is for identity)
Not knowing that reversed() returns an iterator, not a list