Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a list element as reference?

I am passing a single element of a list to a function. I want to modify that element, and therefore, the list itself.

def ModList(element):
    element = 'TWO'

l = list();
l.append('one')
l.append('two')
l.append('three')
print l
ModList(l[1])
print l

But this method does not modify the list. It's like the element is passed by value. The output is:

['one','two','three']
['one','two','three']

I want that the second element of the list after the function call to be 'TWO':

['one','TWO','three']

Is this possible?

like image 862
Ivan Avatar asked Apr 02 '13 21:04

Ivan


People also ask

Are lists passed by reference?

Lists are already passed by reference, in that all Python names are references, and list objects are mutable. Use slice assignment instead of normal assignment. However, this isn't a good way to write a function. You should simply return the combined list.

Is Python list pass by reference?

Python passes arguments neither by reference nor by value, but by assignment.

What is pass by object reference in Python?

What is Pass by Reference In Python? Pass by reference means that you have to pass the function(reference) to a variable which refers that the variable already exists in memory. Here, the variable( the bucket) is passed into the function directly.


2 Answers

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.

like image 122
KDN Avatar answered Oct 11 '22 07:10

KDN


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 into ModList and then modify the element that way

def ModList(theList, theIndex) :
  theList[theIndex] = 'TWO'

ModList(l, 1)
like image 33
JaredPar Avatar answered Oct 11 '22 07:10

JaredPar