Short answer: Not really, but there might be something close enough.


The problem is, under the covers, a is a pointer-to-a-list, and l is a pointer-to-a-list-of-pointers-to-lists. When you a = l[0], what that actually translates to at the CPU is "dereference the pointer l, treat the resulting region of memory as a list object, get the first object (which will be the address of another list), and set the value of pointer a to that address". Once you've done that, a and l[0] are only related by concidence; the are two separate pointers that happen, for the moment, to point at the same object. If you assign to either variable, you're changing the value of a pointer, not the contents of the pointed-to object.

Broadly speaking, there's a few ways the computer could practically do what you ask.

  1. Modify the pointed-to object (list) without modifying either pointer. That's what the append function does, along with the many other mutators of python lists. If you want to do this in a way that perhaps more clearly expresses your intent, you could do l[0][:] = [1,2]. That's a list copy operation, copying into the object pointed to by both l[0] and a. This is your best bet as a developer, though note that copy operations are O(n).
  2. Implement a as a pointer-to-a-pointer-to-a-list that is automatically dereferenced (to merely a pointer-to-list) when accessed. This is not, AFAIK, something Python provides any support for; almost no language does. In C you could say list ** a = &(l[0]); but then any time you want to actually do anything with a you'd have to use *a instead.
  3. Tell the interpreter to observe that a is an alias to l[0], rather than its own, separate variable. As far as I know, Python doesn't support this either. In C, you could do it as #define a (l[0]) though you'd want to #undef a when it went out of scope.
  4. Rather than making a a list variable (which is implemented as a pointer-to-list), make it a function: a = lambda: l[0]. This means you have to use a() instead of a anywhere you want to get the actual content of l[0], and you can't assign to l[0] through a() (or through a directly). But it does work, in Python. You could even go so far as to use properties, which would let you skip the parentheses and assign through a, but at the cost of writing a bunch more code to wrap the lists (I'm not aware of a way to attach properties to lists directly, though one might exist, so you'd instead have to create a new object wrapping the list).
Answer from CBHacking on Stack Overflow
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › list
list — Python Reference (The Right Way) 0.1 documentation
Lists are mutable ordered and indexed collections of objects. The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.)
Top answer
1 of 3
2

Short answer: Not really, but there might be something close enough.


The problem is, under the covers, a is a pointer-to-a-list, and l is a pointer-to-a-list-of-pointers-to-lists. When you a = l[0], what that actually translates to at the CPU is "dereference the pointer l, treat the resulting region of memory as a list object, get the first object (which will be the address of another list), and set the value of pointer a to that address". Once you've done that, a and l[0] are only related by concidence; the are two separate pointers that happen, for the moment, to point at the same object. If you assign to either variable, you're changing the value of a pointer, not the contents of the pointed-to object.

Broadly speaking, there's a few ways the computer could practically do what you ask.

  1. Modify the pointed-to object (list) without modifying either pointer. That's what the append function does, along with the many other mutators of python lists. If you want to do this in a way that perhaps more clearly expresses your intent, you could do l[0][:] = [1,2]. That's a list copy operation, copying into the object pointed to by both l[0] and a. This is your best bet as a developer, though note that copy operations are O(n).
  2. Implement a as a pointer-to-a-pointer-to-a-list that is automatically dereferenced (to merely a pointer-to-list) when accessed. This is not, AFAIK, something Python provides any support for; almost no language does. In C you could say list ** a = &(l[0]); but then any time you want to actually do anything with a you'd have to use *a instead.
  3. Tell the interpreter to observe that a is an alias to l[0], rather than its own, separate variable. As far as I know, Python doesn't support this either. In C, you could do it as #define a (l[0]) though you'd want to #undef a when it went out of scope.
  4. Rather than making a a list variable (which is implemented as a pointer-to-list), make it a function: a = lambda: l[0]. This means you have to use a() instead of a anywhere you want to get the actual content of l[0], and you can't assign to l[0] through a() (or through a directly). But it does work, in Python. You could even go so far as to use properties, which would let you skip the parentheses and assign through a, but at the cost of writing a bunch more code to wrap the lists (I'm not aware of a way to attach properties to lists directly, though one might exist, so you'd instead have to create a new object wrapping the list).
2 of 3
1

If a = l then calling a[0] in that case would yield [1,2], however, you are deleting the list that “a” was referencing, therefore the reference is destroyed as well. You need to either make a = l and call a[0] or reset the reference by calling a = l[0] again.

Discussions

python - How to pass a list element as reference? - Stack Overflow
However, since I have wanted to abuse python in a similar fashion, I will submit this method as a workaround. Calling a specific element from a list directly returns a copy of the value at that element in the list. Even copying a sublist of a list returns a new reference to an array containing ... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I get the reference of a List element? - Stack Overflow
But this local list should only point to a subset of users of the "global" user list. ... Python is an interpreted scripting language and the stuff that you are trying to do needs more "Compiled" type languages like C/C++ where you can reference memory locations using pointers. More on stackoverflow.com
🌐 stackoverflow.com
python - Reference to an element in a list - Stack Overflow
I am a little confused about how python deal with reference to an element in a list, considering these two examples: More on stackoverflow.com
🌐 stackoverflow.com
Python referring to an item in a list from another list - Stack Overflow
I have a list of variables (listA) from from another list (listB). The problem I am having is that the items from listB are being passed by value to listA rather than by reference. Is there anyway ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Google
developers.google.com › google for education › python › python lists
Python Lists | Python Education | Google for Developers
Python's built-in list type is defined using square brackets [ ] and elements are accessed using zero-based indexing. Assigning one list variable to another makes both variables point to the same list in memory.
Top answer
1 of 4
6

The explanations already here are correct. However, since I have wanted to abuse python in a similar fashion, I will submit this method as a workaround.

Calling a specific element from a list directly returns a copy of the value at that element in the list. Even copying a sublist of a list returns a new reference to an array containing copies of the values. Consider this example:

>>> a = [1, 2, 3, 4]
>>> b = a[2]
>>> b
3
>>> c = a[2:3]
>>> c
[3]
>>> b=5
>>> c[0]=6
>>> a
[1, 2, 3, 4]

Neither b, a value only copy, nor c, a sublist copied from a, is able to change values in a. There is no link, despite their common origin.

However, numpy arrays use a "raw-er" memory allocation and allow views of data to be returned. A view allows data to be represented in a different way while maintaining the association with the original data. A working example is therefore

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> b = a[2]
>>> b
3
>>> b=5
>>> a
array([1, 2, 3, 4])
>>> c = a[2:3]
>>> c
array([3])
>>> c[0]=6
>>> a
array([1, 2, 6, 4])
>>> 

While extracting a single element still copies by value only, maintaining an array view of element 2 is referenced to the original element 2 of a (although it is now element 0 of c), and the change made to c's value changes a as well.

Numpy ndarrays have many different types, including a generic object type. This means that you can maintain this "by-reference" behavior for almost any type of data, not only numerical values.

2 of 4
5

Python doesn't do pass by reference. Just do it explicitly:

l[1] = ModList(l[1])

Also, since this only changes one element, I'd suggest that ModList is a confusing name.

Top answer
1 of 2
1

Everything in python is a reference. After all those statements are executed, listB[1] and listA[1] are literally the same object. (you can check, by calling id(listB[1]) and id(listA[1]).

The reason listA[0] and listB[0] are different is merely because you put a different reference into that spot.

Judging from your description, you don't want to a listA that stores references to the objects in listB. What you want is a listA that is a view of listB. I believe you have only two options:

  • Create a special sequence that internally stores a reference to listA, and whose __getitem__ and __setitem__ methods perform lookups into listA when invoked.

  • Create special a special reference types that contains something like a "sequence and index". Put these references into listA. But, to modify listB through listA, you'll have to invoke some sort of "get" and "set" members of these reference objects.

2 of 2
1

You can't. First of all integers are immutable and they don't work like integers in C/C++. You can't get pointer/reference to an integer and then change it (I mean you can, you always have a reference, but it is usually reference to a single object; check this x = 1; y = 1; print id(x), id(y); id values should be the same and they are memory addresses). What you can do is get index of elements in listB and change the list, e.g.:

listA = [0, 1]
listB[listA[0]] = 0

But probably you are trying to do something you are not supposed to do, because Python works differently than C++. What are you trying to achieve?

Find elsewhere
🌐
W3Schools
w3schools.com › python › python_lists.asp
Python Lists
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... Lists are used to store multiple items in a single variable.
🌐
Reddit
reddit.com › r/learnpython › lists reference value
r/learnpython on Reddit: lists reference value
June 12, 2025 -

what does " lists hold the reference of value " mean. i'm a total beginner in programming, and i'm learning python, and i passsed by through this which i didn't understand.
any help please.

Top answer
1 of 5
4
Would be better to know the context. But basically lists do not know what they contain, but lists only know that something is stored in a place and accordingly the position. This reference or position can then be used to retrieve the stored data.
2 of 5
3
A reference is the address in memory which a value exists. A Python list itself isn’t a list of all of those values one after another in memory, but rather a list of references to various values elsewhere in memory. This makes it easier to create, modify and change the list (because a reference is the same size no matter the value, while different data types can be of different sizes). Indeed all Python variables work via references behind the scenes. Mutable and immutable types is what determines if you new instance of a value needs to be created when modified or if the value in memory can be modified directly. List are mutable, this is why you can create a list A and then make another list B that is set equal to A; then modify B by say adding a element (since it is the same reference as A it modifies the same list); then print A and see that new element you added with B. This because when you set B equal to A, you didn’t copy the list but just the reference. So now B and A are referencing the same memory address (ie same list in memory). But something like a string is immutable so if you created string A and then String B and modify B and print A, you will not see the modification because when you modified B it actually created a completely new string value in memory and B is referencing that one instead now.
🌐
Dummies
dummies.com › article › technology › programming-web-design › python › how-to-define-and-use-python-lists-264919
How to Define and Use Python Lists | dummies
October 9, 2019 - Each item in a list has a position number, starting with zero, even though you don’t see any numbers. You can refer to any item in the list by its number using the name for the list followed by a number in square brackets.
Author:
🌐
CodeRivers
coderivers.org › blog › how-to-reference-an-element-in-a-list-python
Python Lists: How to Reference Elements - CodeRivers
April 10, 2025 - Negative indexing is also possible, where the last element has an index of -1, the second last has an index of -2, and so forth. Positive indexing starts from 0. To reference an element in a list using positive indexing, you simply use the index value inside square brackets after the list name.
🌐
Reddit
reddit.com › r/learnpython › reference items in a list by reference instead of index?
r/learnpython on Reddit: Reference items in a list by reference instead of index?
July 13, 2024 -

This one might be a bit hard to describe, but the high-level logic is...

For a given list of random numbers (n), take the 20th value, and determine what percentage of the previous 19 numbers are below the 20th value. Repeat this for the 21st value, 22nd, etc. It will always be the previous 19 numbers.

I've got an algorithm that does this, but I need to iterate over a stupidly large amount of data. If I ran it right now, even with splitting across 6 processes, it's still going to take 2 weeks to complete. That's not practical for my purposes.

I came up with a faster algorithm, but I'm having trouble implementing it. It involves tracking merely the change in the number being compared to so that it doesn't have to iterate over all previous 19 numbers. This would scale very for what I need and should reduce the number of times I have to iterate over the data by billions.

I can almost achieve this by tracking the indexes of the items I'm looking at. The problem is that I have to insert items into the middle of this list which invalidates the index I had saved because the real value got bumped up to a higher index. If I could simply use it by reference, this wouldn't happen.

I've considered using dictionaries, but that would involve me making keys out of every number in the system down to at least 0.0001. That doesn't seem practical and I'm skeptical it would even work.

Hope I explained this well. Would appreciate any help.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lists
Python Lists - GeeksforGeeks
2. Using list() Constructor: A list can also be created by passing an iterable (such as tuple, string or another list) to the list() constructor. ... 3. Creating List with Repeated Elements: A list with repeated elements can be created using the multiplication (*) operator. ... Python list stores references to objects, not the actual values directly.
Published: 2 weeks ago
Top answer
1 of 2
4

You misunderstood. There are 5 distinct references. If those 5 references all point to the same nested list and you alter that nested list, you can see the change reflected through all those references.

You didn't change a nested list here. You changed one of the references.

Think of references as nametags. You can put more than one nametag on an object:

nametag_a+-----------+
                 +---v--+
                 |object|
                 +---^--+
                     |
nametag_b+-----------+

You can 'look' at the object through either reference. Assignment is simply attaching a reference to an object. If a reference pointed to an object before, it is detached from that object and now points to another object:

nametab_b = another_object

results in

nametag_a+-----------+
                 +---v--+
                 |object|
                 +------+

nametag_b    +--------------+
    +-------->another_object|
             +--------------+

The numbered indices in a list are references to; so instead of nametag_a, you have 0, and 1, etc.

The other question talks about nested lists. There you have multiple references to a single list object:

# indices in a list on the left referencing another list

0+---------------------------+
                             |
                             |
1+------------------------+  |
                          |  |
                +---------v--v-----------+
2+--------------> list with more indices |
                +---------^--^-----------+
                          |  |
3+------------------------+  |
                             |
                             |
4+---------------------------+

If you make a change to the list with more indices contents, then you'll see those changes through any of the 5 references in the outer list

Please read Facts and myths about Python names and values by Ned Batchelder, which explains this in more detail.

2 of 2
0

Great site to further understanding the situation.

Python Tutor

🌐
Reddit
reddit.com › r/learnpython › creating lists containing references to items in another list
r/learnpython on Reddit: creating lists containing references to items in another list
June 30, 2021 -

Sorry if this is something I should be able to find on my own, but I'm not finding what I need elsewhere.

I have a list A containing nine integers. I want to create a second list B such that it's values are references to A[1:8:3]. In essence, if the value stored in A[1] is changed, I want the value of B[0] to change automatically.

I hope that makes sense.

🌐
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.