You did not modify the elements of the original array, but rather re-assigned a new list to the arr variable. Your intuition of thinking changes to elements would be reflected in arr1 if you properly accessed its elements is indeed true as lists are mutable in Python. For instance,

arr = [1,2,3]
arr1 = arr
arr[1] = 4
print(arr1) #this prints [1,4,3]
Answer from miradulo on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-reference-elements-in-an-array-in-python
How to Reference Elements in an Array in Python - GeeksforGeeks
July 23, 2025 - For array referencing only the index of the required element has to be passed to the name of the array.
๐ŸŒ
Learning About Electronics
learningaboutelectronics.com โ€บ Articles โ€บ How-to-reference-elements-in-an-array-in-Python.php
How to Reference Elements in an Array in Python
And this is how you can create a copy of an array in Python. So to reference an element of a two-dimensional array, the first parameter is the row that you are referencing and the second parameter is the column you are referencing.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ alias or reference an array element from another array
r/learnpython on Reddit: alias or reference an array element from another array
January 21, 2024 -

Apologies if this is mentioned somewhere else. I have been searching the web but not found an example that helped me understand.

I would like to create an array where each element is a reference to an element in another array. Syntax may not be exact but the general idea is:

a = [1, 2, 3]
b = []
b.append(a[2]) #create references to a but reverse the order
b.append(a[1]) #this would be done dynamically in the real code
b.append(a[0])
a[0] = 10

Now I would like a situation where because I set b[2] equal to a[0], when a[0] = 10, b[2] 'points' to it and asking for b[2] also gives 10. Is this possible? Do I have to create array b in a different way?

Thanks in advance for any help. I read all comments even if a solution is already found.

๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arrays.asp
Python Arrays
You refer to an array element by referring to the index number.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ array.html
array โ€” Efficient arrays of numeric values
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ problem when referencing an array
r/learnpython on Reddit: Problem when referencing an array
October 12, 2023 -

I have been learning how to use 2D arrays in python this year for my computer science class. I thought I understood them until I tried using them in my Tkinter program which will hopefully eventually become battleship for an assignment. I asked my teacher if he knew the problem here, but he is also stumped. I am trying to reference a value in an array in a function, but every time the value is called in the function, the first value is messed up. I am not sure what the problem is, and neither of us could understand it. When a button is pushed, the terminal should print the values listed on the button, however the first value is always 6. If anyone could suggest anything I am more than welcoming of feedback at this point. My entire program is outlined below. Thanks in advance!

from tkinter import *

from tkinter import ttk import platform

opSys = platform.system()

def clicked(b): info = b.grid_info() print ("\n") print (str(info["column"]) + ", " + str(info["row"])) print (playerGridArray[(info["column"])][(info["row"])]) print (playerGridArray[2][0])

playerGridArray = [[0]*7]*7

win = Tk() win.title("Battleship") win.config(bg="light sky blue")

x = 0 y = 0 n = 1

while y < 7: b = Button(win, text=(str(x) + ", " + str(y)), width = 3, height = 2, ) b["command"] = lambda b=b: clicked(b)

b.grid(row = y, column = x)
print (str(x) + " " + str(y))
playerGridArray[x][y] = str(x) + ", " + str(y)
print(playerGridArray[x][y])

n = n + 1
if x == 6:
    x = 0
    y = y + 1
else:
    x = x + 1

๐ŸŒ
Data8
data8.org โ€บ fa19 โ€บ python-reference.html
Python Reference
In the examples in the left column, np refers to the NumPy module, as usual. Everything else is a function, a method, an example of an argument to a function or method, or an example of an object we might call the method on. For example, tbl refers to a table, array refers to an array, and num ...
๐ŸŒ
Readthedocs
ecco-v4-python-tutorial.readthedocs.io โ€บ ECCO_v4_Operating_on_Numpy_Arrays.html
Operating on Numpy arrays โ€” ECCO Version 4 Python Tutorial 4.4.1 documentation
Unlike some other languages, creating a new variable with an assignment statement in Python such as x = some_numpy_array ยท does not make a copy of some_numpy_array. Instead, the assignment statement makes x and some_numpy_array both point to the same numpy array in memory. Because x and some_numpy_array are both refer (or pointer) to the same numpy array in memory, the numpy array can be changed by operations on either x or some_numpy_array.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.array.html
numpy.array โ€” NumPy v2.5 Manual
Setting ndmax stops recursion at the specified depth, preserving deeper nested structures as objects instead of promoting them to higher-dimensional arrays. In this case, dtype=np.object_ is required. New in version 2.4.0. ... Reference object to allow the creation of arrays which are not NumPy arrays.
๐ŸŒ
i-Programmer
i-programmer.info โ€บ programming โ€บ python โ€บ 3942-arrays-in-python.html
Arrays in Python
March 19, 2012 - Programming book reviews, programming tutorials,programming news, C#, Ruby, Python,C, C++, PHP, Visual Basic, Computer book reviews, computer history, programming history, joomla, theory, spreadsheets and more.
Top answer
1 of 3
98

In Python, all variable names are references to values.

When Python evaluates an assignment, the right-hand side is evaluated before the left-hand side. arr - 3 creates a new array; it does not modify arr in-place.

arr = arr - 3 makes the local variable arr reference this new array. It does not modify the value originally referenced by arr which was passed to foo. The variable name arr simply gets bound to the new array, arr - 3. Moreover, arr is local variable name in the scope of the foo function. Once the foo function completes, there is no more reference to arr and Python is free to garbage collect the value it references. As Reti43 points out, in order for arr's value to affect a, foo must return arr and a must be assigned to that value:

def foo(arr):
    arr = arr - 3
    return arr
    # or simply combine both lines into `return arr - 3`

a = foo(a)

In contrast, arr -= 3, which Python translates into a call to the __iadd__ special method, does modify the array referenced by arr in-place.

2 of 3
11

Python passes the array by reference:

$:python
...python startup message

>>> import numpy as np
>>> x = np.zeros((2,2))
>>> x
array([[0.,0.],[0.,0.]])
>>> def setx(x):
...    x[0,0] = 1
...
>>> setx(x)
>>> x
array([[1.,0.],[0.,0.]])

The top answer is referring to a phenomenon that occurs even in compiled c-code, as any BLAS events will involve a "read-onto" step where either a new array is formed which the user (code writer in this case) is aware of, or a new array is formed "under the hood" in a temporary variable which the user is unaware of (you might see this as a .eval() call).

However, I can clearly access the memory of the array as if it is in a more global scope than the function called (i.e., setx(...)); which is exactly what "passing by reference" is, in terms of writing code.


And let's do a few more tests to check the validity of the accepted answer:

(continuing the session above)
>>> def minus2(x):
...    x[:,:] -= 2
...
>>> minus2(x)
>>> x
array([[-1.,-2.],[-2.,-2.]])

Seems to be passed by reference. Let us do a calculation which will definitely compute an intermediate array under the hood, and see if x is modified as if it is passed by reference:

>>> def pow2(x):
...    x = x * x
...
>>> pow2(x)
>>> x
array([[-1.,-2.],[-2.,-2.]])

Huh, I thought x was passed by reference, but maybe it is not? -- No, here, we have shadowed the x with a brand new declaration (which is hidden via interpretation in python), and python will not propagate this "shadowing" back to global scope (which would violate the python-use case: namely, to be a beginner level coding language which can still be used effectively by an expert).

However, I can very easily perform this operation in a "pass-by-reference" manner by forcing the memory (which is not copied when I submit x to the function) to be modified instead:

>>> def refpow2(x):
...    x *= x
...
>>> refpow2(x)
>>> x
array([[1., 4.],[4., 4.]])

And so you see that python can be finessed a bit to do what you are trying to do.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 62687106 โ€บ referential-arrays-in-python
Referential Arrays in Python - Stack Overflow
Python could attempt to reserve enough space for each cell to hold the maximum length string (not just of currently stored strings, but of any string we might ever want to store), but that would be wasteful. Instead, Python represents a list or tuple instance using an internal storage mechanism of an array of object references.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ array โ€บ array-indexing-in-python
Array Indexing in Python - Beginner's Reference - AskPython
February 22, 2021 - We use cookies to ensure that we give you the best experience on our website. ... Array Indexing means searching for elements in an array using the index (position) of elements for quick retrieval of information. Python arrays are variables that consist of more than one element.
๐ŸŒ
W3schools
w3schools.dev โ€บ python โ€บ python_arrays.asp
Python Arrays - W3Schools
You refer to an array element by referring to the index number.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ array
Python Array of Numeric Values
Note: When people say arrays in Python, more often than not, they are talking about Python lists. If that's the case, visit the Python list tutorial. To create an array of numeric values, we need to import the array module.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_list.asp
Python List/Array Methods
Python has a set of built-in methods that you can use on lists/arrays.