Python Arrays & Lists (1D & 2D)
Python Arrays & Lists Interview Questions
| Aspect | Python List | Array (array module/numpy) |
|---|---|---|
| Type | Built-in data type | From array module or numpy |
| Data types | Can store different types | Stores same type elements |
| Memory | More memory overhead | Memory efficient |
| Operations | Basic operations | Mathematical operations |
| Performance | Slower for numerical ops | Faster for numerical ops |
| Use case | General purpose | Numerical computations |
Outer list holds three references to the same inner list—mutating one row shows up on every “row”.
# Bug: 3x the same list object
row = [[]] * 3
row[0].append(1) # row → [[1], [1], [1]]
# Fix: independent inner lists
row = [[] for _ in range(3)]None—sorts in place. Assigning to a variable loses the list unless you chain wrongly. Use sorted(x) for a new list.
Extends in place (a.extend(b)) if b is iterable—same id unless a was rebound via augmented assignment on immutable types (different story).
[1, 2, 3]—in-place extend mutates the shared list. Contrast y = y + [3] which can rebind y to a new list without changing x unless reassigned.
Shifts all elements—amortized O(n). Prefer collections.deque for frequent prepends.
Shallow copy duplicates outer list but inner lists are shared—deepcopy recursively clones. Know circular reference handling cost.
No—lists are mutable and unhashable. Use tuple if elements are hashable, or serialize/freeze pattern.
ValueError—empty sequence. Guard with default or check length first.
Element-wise like dictionary ordering—[1, 10] < [1, 2] is False because 10 > 2 at second position.
Creates new list walking backward—common idiom s[::-1] for reversal without mutating original.
Binary-search insertion index in O(log n)—maintain sorted order efficiently vs linear scan or resorting.
Store negated values or use key transformation—Python’s API is min-oriented.
Index starts at 1 for human-facing numbering—don’t mix up with zero-based slice indexing of xs.
Identity order is arbitrary across runs—use meaningful keys; id ties unstable semantics confuse debugging.
Removes and returns last item—stack semantics. IndexError if empty.
pop returns removed value; del is statement without return—choose based on needing the value.
Deep nesting blows recursion depth—use iterative stack/queue or generators with explicit control.
Compact homogeneous numeric storage (typecode)—less flexible API, better memory for huge numeric buffers.
Zero-copy slicing over bytes/array—performance for binary protocols without copying subarrays.
Lazy flatten one level—memory friendly vs building giant intermediate lists.
Pushes drop opposite end automatically—bounded queues without manual trim.
if not lst: is idiomatic—avoid len(lst) == 0 unless you need explicit length semantics.
Primitives duplicated safely; mutable inner objects repeat references—same [[]]*n pitfall.
Python 3 comprehensions have local scope—loop variable doesn’t leak into enclosing scope (unlike Python 2).
None—shuffles in place; common mistake expecting new list.
Average O(1) vs O(n)—membership in large collections dominates dedup and lookup loops.
lst[s] where s = slice(1, None, 2)—programmatic slicing for dynamic APIs.
Iterator—consume once or wrap in list(); doesn’t mutate lst.
Value equality True—distinct objects can compare equal; is checks identity only.
Compares first elements, then second, lexicographically—exploited in Kruskal-style algorithms and ranking keys.