Concepts / Array Variables and References

Array Variables and References

Assigning one array variable to another copies the reference, so both variables refer to the same array object.

  • Programming

Why Two Names Can Share One Array

Many learners first meet assignment with plain integers. When an integer is assigned to a second variable, the second variable receives its own independent value, so a later change to one variable never touches the other. It is tempting to carry that same picture over to arrays. The belief goes like this: assigning one array variable to another gives you a duplicate array, and each variable now owns a separate collection of elements. This belief feels reasonable, and it is the single most common source of confusion when array code produces a value nobody expected.

The belief is wrong for arrays. Assigning one array variable to another copies the reference, not the array, so both variables refer to the same array object. The goal of this article is not to memorize that rule but to be able to look at an assignment line, know what it did, and predict the consequences for every later read and write.

What do you think happens?

Before reading on, commit to a prediction. What does the final line print: 10, 99, or something else?

  • 10

  • 99

  • something else

Reveal answer
Answer:

99

The assignment b = a copies the reference, so a and b both name the same list. Writing b[0] = 99 changes the one shared array, and reading a[0] then sees 99. The independent-copy belief predicts 10, which is the value a would keep if b had received its own duplicate.

Tracing the Assignment Line by Line

Read the program below one line at a time. For each line, ask two questions: which array object does each variable currently name, and which earlier statement last changed the element you are about to read. Do not run ahead to the answer. The point of the trace is to follow the reference model step by step.

Code walkthrough
a = [1, 2, 3]
b = a
b[1] = 50
print(a[1])
  1. 1
    After line 1, a names the array with elements 1, 2, 3.
  2. 2
    After line 2, a and b both name that same array. The count of array objects is still one.
  3. 3
    After line 3, the shared array holds 1, 50, 3. Both names now see this state.
  4. 4
    Line 4 prints 50. The unexpected value first appears on line 3, where the write through b changed the array that a also reads.
  • Line 1 creates one array object with the elements 1, 2, 3 and binds the name a to it.
  • Line 2 copies the reference stored in a into b. No elements are copied and no second array is created.
  • Line 3 writes through the name b, which reaches the same shared array that a names.
  • Line 4 reads through the name a and sees 50, the value written through b.
Output
50
line 1a = [1, 2, 3]line 2b = aline 3b[1] = 50line 4print a[1] gives 50a and b now name one arraywrite through bread through a
At each line of the program, what does each variable refer to, and on which line does the unexpected value first show up?

The Reference Model and the Shared Object

The variable names in an array program are labels attached to one shared object. Picture two sticky notes that both point at the same filing cabinet. Writing on the cabinet through either note changes what anyone sees in the cabinet, and adding a second note does not duplicate the cabinet. Once you hold this picture, the behavior of the assignment follows directly rather than feeling like an exception.

The assignment statement has a precise effect that is easy to state. It takes the reference value stored in the right-hand variable and stores that same reference value in the left-hand variable. The left-hand variable becomes a second name for the array that the right-hand variable already named. No element is moved, no element is duplicated, and no new array is allocated by that line.

arr1shared array objectelements 1, 2, 3arr2refers torefers to
After arr2 = arr1, what do both variables point to, and why does one variable's change appear in the other?

The structure has two variable names and one array object. The count of array objects is the important number. It stays at one because the assignment never built a second one. Any read or write through either name reaches the same set of elements, so the question to ask about any array variable is not what it holds but which array object it currently names.

Reference Copy Versus Independent Copy

Two different operations can both look like making a copy. A reference copy is a plain assignment that adds a second name for the same array. An independent copy is a new array object, with its own elements, filled with values taken from the original. The table below contrasts what each operation produces.

QuestionReference copy (assignment)Independent copy
What is created?A second name for the existing arrayA new array object
Are elements duplicated?NoYes, each value is copied into the new array
Array objects after the operationOneTwo
Does a write through one name affect the other?Yes, both names see the changeNo, the two arrays are separate

Reference copy versus independent copy of an array

python
Output (expected)
1
99
Notes on the code
  • The new list starts empty, so it is a new array object rather than a second name.
  • The loop copies each value from original into the new array, one element at a time.
  • Writing 99 into independent does not reach original, because the two objects are separate.

Writing Through One of Two Names

Now consider the write itself. Suppose arr1 and arr2 both name the same array, and an element is changed through arr1. The change is stored in the shared array, so reading the same index through arr2 returns the new value. The before-and-after view below shows which stored values change.

arr1[0]1arr1[0]99arr2[0]1arr2[0]99write 99 to shared arraysame shared array
If arr1[0] is changed, what value does arr2[0] show afterward, and which stored array changed?
Worked example

Predicting a read through the other name

Given arr1 = [5, 6, 7], then arr2 = arr1, then arr1[0] = 0, what does arr2[0] return?

  1. 1
    Step 1: identify the object

    After arr2 = arr1, both names refer to the single array with elements 5, 6, 7.

  2. 2
    Step 2: apply the write

    arr1[0] = 0 changes the element at index 0 of that shared array to 0.

  3. 3
    Step 3: read through the other name

    arr2 names the same array, so arr2[0] reads the element now holding 0.

Result

arr2[0] returns 0, and the stored array is now 0, 6, 7 for both names.

Null References Through Assignment

A variable that holds a reference can also hold null, which means it refers to no array at all. The same reference-copying rule applies. If the right-hand variable is null, the left-hand variable also becomes null after the assignment. The two variables still agree about what they refer to, and what they agree on is nothing.

Consider a variable named data that is null and is then assigned to a second variable named backup. After the assignment, backup also refers to no array. Trying to read an element through backup does not return a wrong value. It fails, because there is no array to read from in the first place. Keep these two cases separate when you debug: a wrong value comes from a shared array that was changed earlier, while a failure from a null reference means no array was ever attached to the name.

Mistakes Beginners Make with Array Assignment

  • Common mistake

    Assuming that assigning one array variable to another duplicates the elements

    What it looks like
    a = [1, 2, 3]; b = a; b[0] = 99; print(a[0]) is expected to print 1
    Why it fails

    Assignment copies the reference, so a and b name the same array object. The write to b[0] is visible through a, and the program prints 99.

    Do this instead

    Read the assignment as adding a second name. Ask which array object each name currently refers to before predicting a read.

  • Common mistake

    Treating a null reference as an empty array

    What it looks like
    data = None; backup = data; then reading backup[0] and expecting an empty result
    Why it fails

    A null reference refers to no array at all. An empty array is still an array object. Indexing through the null reference fails because there is no array to read from.

    Do this instead

    Check whether the variable is null before indexing it, and distinguish a failure from an unexpected stored value.

  • Common mistake

    Looking for the bug on the line where the wrong value is printed

    Why it fails

    The printed value may be correct for its own line. The change that caused the surprise usually happened earlier, through another name.

    Do this instead

    Trace backward: for each variable, find the array it names and the last statement that changed the element being read.

  • Common mistake

    Expecting an independent copy from a plain assignment without creating a new array and copying the values

Practice Tracing a Shared Array

MEDIUM

Trace the program below. Write down which array object x and y name after each line, and state the value printed on the last line. Then identify the first line where the printed value differs from what a plain integer assignment would produce.

x = [4, 5, 6]
y = x
x[2] = 0
y = [7, 8, 9]
print(x[2])
Hints
  • After line 2, x and y name the same array. Ask which array line 3 changes.
  • Line 4 binds y to a new array object. It does not change the array that x names.
  • The printed value comes from x, so trace what x still refers to on line 5.

Key Takeaways

  • Assigning one array variable to another copies the reference, so both variables refer to the same array object and no elements are duplicated.
  • A write through either variable changes the single shared array, and both variables see that change when they read it.
  • A null array reference refers to no array, and the same reference-copying rule applies, so the second variable is also null.
  • To debug array code, ask which array object each variable names, then find the last statement that changed the element being read.