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 Overflowpython - How can I get the reference of a List element? - Stack Overflow
python - Using an index to get an item - Stack Overflow
python - How to pass a list element as reference? - Stack Overflow
Reference items in a list by reference instead of index?
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.
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.
As jonrsharpe said, you can do what you want by using mutable elements in your list, eg make the list elements lists themselves.
For example
a = [[i] for i in xrange(5)]
print a
b = a[3]
print b[0]
a[3][0] = 42
print a
print b[0]
b[:] = [23]
print a
print b
output
[[0], [1], [2], [3], [4]]
3
[[0], [1], [2], [42], [4]]
42
[[0], [1], [2], [23], [4]]
[23]
What you show, ('A','B','C','D','E'), is not a list, it's a tuple (the round parentheses instead of square brackets show that). Nevertheless, whether it to index a list or a tuple (for getting one item at an index), in either case you append the index in square brackets.
So:
thetuple = ('A','B','C','D','E')
print thetuple[0]
prints A, and so forth.
Tuples (differently from lists) are immutable, so you couldn't assign to thetuple[0] etc (as you could assign to an indexing of a list). However you can definitely just access ("get") the item by indexing in either case.
values = ['A', 'B', 'C', 'D', 'E']
values[0] # returns 'A'
values[2] # returns 'C'
# etc.
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.
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.