It's not possible, because integers are immutable, while list are mutable.

In b = a[1] you are actually assigning a new value to b

Demo:

>>> a = 2
>>> id(a)
38666560
>>> a += 2
>>> id(a)
38666512

You can like this,

>>> a = [1,2,3]
>>> b = a
>>> a[1] = 3
>>> b
[1, 3, 3]
>>> a
[1, 3, 3]
>>> id(a)
140554771954576
>>> id(b)
140554771954576

You can read this document.

Answer from Adem ร–ztaลŸ on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_index.asp
Python List index() Method
Remove List Duplicates Reverse ... Plan Python Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
Discussions

python - How can I get the reference of a List element? - Stack Overflow
You can't assign a reference from a variable to a list position. Unlike C++, in Python this is not possible. The closest you can go is to create an anonymous function to reference that single list index, and assign a new value to it: More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Using an index to get an item - Stack Overflow
I have a list in python ('A','B','C','D','E'), how do I get which item is under a particular index number? Example: Say it was given 0, it would return A. Given 2, it would return C. Given 4, it w... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to pass a list element as reference? - Stack Overflow
Python doesn't do pass by reference. Just do it explicitly: ... Also, since this only changes one element, I'd suggest that ModList is a confusing name. ... Save this answer. ... Show activity on this post. Python is a pass by value language hence you can't change the value by assignment in the function ModList. What you could do instead though is pass the list and index ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Reference items in a list by reference instead of index?
I've got an algorithm that does this Your algorithm? More on reddit.com
๐ŸŒ r/learnpython
7
3
July 13, 2024
๐ŸŒ
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.

๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list โ€บ index
Python List index() (with Code Visualization)
The index() method returns the index of a specified item in the list. If there are multiple matching items, it returns the index of the first occurrence of the item. Here's a quick example.
๐ŸŒ
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.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ how-to-reference-an-element-in-a-list-python
Python Lists: How to Reference Elements - CodeRivers
August 8, 2026 - 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.
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Python's built-in index() function is a useful tool for finding the index of a specific element in a sequence. This function takes an argument representing the value to search for and returns the index of the first occurrence of that value in the sequence. If the value is not found in the sequence, ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.8 documentation
3 days ago - Remove all items from the list. Similar to del a[:]. ... Return zero-based index of the first occurrence of value in the list.
Top answer
1 of 4
7

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
6

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.

๐ŸŒ
CodeGenes
codegenes.net โ€บ blog โ€บ how-to-reference-an-element-in-a-list-python
Mastering Element Referencing in Python Lists โ€” codegenes.net
Indexing is the most basic way to reference an element in a list. You can use square brackets [] after the list name, followed by the index of the element you want to access. # Create a list my_list = [10, 20, 30, 40, 50] # Access the first ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list
Python Find in List โ€“ How to Find the Index of an Item or Element in a List
February 24, 2022 - Use the index() method to find the index of an item 1.Use optional parameters with the index() method ยท Get the indices of all occurrences of an item in a list ยท Use a for-loop to get indices of all occurrences of an item in a list ยท Use ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
This method modifies the original list by appending all items from the given iterable. Using extend() method is easy and efficient way to merge two lists or add multiple elements at once.Letรขย€ย™s look at a simple ยท 2 min read Python List index() - Find Index of Item
Published: April 27, 2025
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_access.asp
Python - Access List Items
List items are indexed and you can access them by referring to the index number.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_lists.htm
Python - Lists
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. For example โˆ’ ยท list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ...
๐ŸŒ
W3schools
w3schools.dev โ€บ python โ€บ ref_list_index.asp
Python List index() Method - W3Schools
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Server Python Interview Q&A Python Bootcamp Python Certificate ... The index() method returns the position at the first occurrence of the specified value.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ list
list โ€” Python Reference (The Right Way) 0.1 documentation
Extends the list by appending all the items from the iterable. ... Removes the first item from the list which matches the specified value. ... Removes and returns the item at the specified index.
๐ŸŒ
Toppr
toppr.com โ€บ guides โ€บ python-guide โ€บ references โ€บ methods-and-functions โ€บ methods โ€บ list โ€บ index โ€บ python-list-index
Python List index() function | Why do we use Python list index() function? |
September 20, 2021 - The Python list index() function is used to return the index value of the specified element in the list. The Python list index() is a built-in function that searches for a specified element from the beginning of the list and returns the lowest ...