DEV Community

Cover image for 5 Python List Problems That Catch Everyone Off Guard
Ameer Abdullah
Ameer Abdullah

Posted on

5 Python List Problems That Catch Everyone Off Guard

Lists are the first data structure you learn in Python. They are also the source of the most interview mistakes. These five problems test list behavior that most developers misunderstand until they get it wrong under pressure.

Work through each one before reading the answer.


Problem 1: The Slice That Does Nothing

a = [1, 2, 3, 4, 5]
b = a[:]
b.append(6)
print(a)
print(b)
Enter fullscreen mode Exit fullscreen mode

What do both lines print?

Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

a[:] creates a shallow copy. b is a new list object containing the same integer objects. Appending to b only modifies b because integers are immutable and the copy itself is a different object than a.


Problem 2: The Shallow Copy Trap

a = [[1, 2], [3, 4], [5, 6]]
b = a[:]
b[0].append(99)
print(a)
print(b)
Enter fullscreen mode Exit fullscreen mode

Output:

[[1, 2, 99], [3, 4], [5, 6]]
[[1, 2, 99], [3, 4], [5, 6]]
Enter fullscreen mode Exit fullscreen mode

The shallow copy makes a new outer list but the inner lists are the same objects. b[0] and a[0] both point to the same inner list. Mutating through b[0] changes the object that a[0] also points to.

To avoid this you need copy.deepcopy(a).


Problem 3: The Multiplication Surprise

matrix = [[0] * 3] * 3
matrix[0][1] = 9
print(matrix)
Enter fullscreen mode Exit fullscreen mode

Most people expect:

[[0, 9, 0], [0, 0, 0], [0, 0, 0]]
Enter fullscreen mode Exit fullscreen mode

Actual output:

[[0, 9, 0], [0, 9, 0], [0, 9, 0]]
Enter fullscreen mode Exit fullscreen mode

The * operator on a list creates multiple references to the same inner list, not copies of it. All three rows are the same object. Modifying one modifies all.

The correct way to create a 2D matrix:

matrix = [[0] * 3 for _ in range(3)]
Enter fullscreen mode Exit fullscreen mode

List comprehension creates a new list for each row.


Problem 4: The Sort That Returns Nothing

numbers = [3, 1, 4, 1, 5, 9]
result = numbers.sort()
print(result)
print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output:

None
[1, 1, 3, 4, 5, 9]
Enter fullscreen mode Exit fullscreen mode

list.sort() sorts in place and returns None. sorted(numbers) returns a new sorted list without modifying the original. This confusion between the two is one of the most common bugs data science developers introduce when cleaning datasets.


Problem 5: The Remove During Iteration Bug

numbers = [1, 2, 3, 4, 5, 6]
for n in numbers:
    if n % 2 == 0:
        numbers.remove(n)
print(numbers)
Enter fullscreen mode Exit fullscreen mode

Most people expect: [1, 3, 5]

Actual output: [1, 3, 5, 6]

The 6 survives. Here is why. When you remove 2 from index 1, all elements shift left. The index counter advances to 2, which now points to 4 (not 3). 3 is never checked. Then 4 is removed and 6 shifts to index 3, but the counter advances to 4 which is past the end. 6 is never checked.

The safe pattern:

numbers = [n for n in numbers if n % 2 != 0]
Enter fullscreen mode Exit fullscreen mode

Always use list comprehension or iterate over a copy when removing during iteration.

Practice predicting outputs like these at PyCodeIt. Free, no account needed to start.

Now v 2.0 is available. Go and check and give feedback!

Top comments (1)

Collapse
 
roghayem profile image
Roghaye Mohammadi

I made the same mistakes when I started learning Python!