Python variables hold references to objects. Assigning a list to another name does not copy the list. That is the key to interview questions about aliases, mutation and copies. Before predicting output, draw the objects and mark which names point to each one.

This beginner exercise uses Python 3 syntax and invented shopping data. It does not depend on a particular company's interview process. The aim is to explain why a value changes, not memorize the printed result.

Does assignment create a new list?

No. In the first part of this example, both names point to one outer list. Changing its contents is visible through either name. A new name is not a new storage container.

import copy
items = [["tea", 1], ["rice", 2]]
alias = items
outer_copy = items.copy()
independent = copy.deepcopy(items)
items[0][1] = 3
assert alias is items
assert outer_copy is not items
assert outer_copy[0] is items[0]
assert outer_copy[0][1] == 3
assert independent[0][1] == 1
outer_copy.append(["salt", 1])
assert len(items) == 2
assert len(outer_copy) == 3

The assertions express the expected results. The copied outer list has its own length, but its first element still refers to the same inner list as the original. This is why changing the quantity appears in the shallow copy while appending an extra row does not affect the original outer list.

What does a shallow copy actually copy?

A shallow copy makes a new container and fills it with references to the original elements. It does not recursively copy every object reachable from those elements. A list of integers can hide this difference because integers cannot be mutated in place. Nested mutable objects make the sharing easier to see.

Ask which level must be independent. If a function only adds or removes outer rows, a shallow copy might provide the required boundary. If it edits the rows themselves, the same copy is not enough. The answer depends on the operations, not the word “copy” alone.

Is a deep copy always the right solution?

No. It can be useful when the required result is an independent nested object graph. It also does extra work and may not represent what the application needs. Files, connections and objects with custom copying behavior need more thought than a small list of lists.

For this exercise, each row contains only a string and an integer. A list comprehension that explicitly builds the rows is another understandable option. Do not claim that every nested structure can be safely copied with the same expression.

How is rebinding different from mutation?

Mutation changes an existing object. Rebinding changes which object a name refers to. If you assign a completely new list to items, alias still points to the old list. It does not automatically follow the new binding.

In an interview, describe the exact operation. “This assignment changes the name's reference” is clearer than “Python passes everything by reference,” a phrase that often mixes up binding, argument passing and mutation.

Questions to practise aloud

  • Why does the shallow copy see the changed quantity?
  • Why does appending a row affect only one outer container?
  • What would change if each row were an immutable tuple?
  • Which independence does the caller actually require?

A good answer names the shared object, identifies the mutation and checks an observable result. If you are uncertain, run a smaller example and inspect identity with is. Use equality to compare values; do not substitute identity checks for value comparisons in normal program logic.

Common mistake: multiplying nested lists

Repeating a list that contains one mutable row can repeat references to that row. Editing one apparent row may therefore change several positions. Build each row separately when separate rows are required. Explain the object graph instead of treating the behavior as a mysterious list exception.

For your next practice attempt, replace the shopping rows with two small dictionaries. Predict what a shallow copy shares before running it. That variation tests the same idea without requiring a new rule.

References and next practice

docs.python.org — py data · docs.python.org — py model

Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud