4

Is there any way to pass a pointer to a list, so I could have update_list(list, data)

Instead of having to do list = update_list(list, data)

Independent of whether this is possible, what is advisable and Pythonic in this situation?

Ben G
  • 26,091
  • 34
  • 103
  • 170
  • I'm wondering if your data is also a list? If so, you might consider original_list.extend(new_list). This would be very "pythonix", because its simple, explicit and beautiful. ;-) – Don Question Dec 24 '11 at 08:21
  • also something as simple as `list += data` may work for you if you are just extending the list – John La Rooy Dec 24 '11 at 22:00

5 Answers5

10

I recommend reading Semantics of Python variable names from a C++ perspective:

All variables are references

This is oversimplification of the entire article, but this (and the understanding that a list is a mutable type) should help you understand how the following example works.

In [5]: def update_list(lst, data):
   ...:     for datum in data:
   ...:         lst.append(datum)
   ...:         

In [6]: l = [1, 2, 3]

In [7]: update_list(l, [4, 5, 6])

In [8]: l
Out[8]: [1, 2, 3, 4, 5, 6]

You can even shorten this by using the extend() method:

In [9]: def update_list(lst, data):
   ...:     lst.extend(data)
   ...:       

Which actually probably removes the need of your function.

N.B: list is a built-in and therefore a bad choice for a variable name.

Bite code
  • 578,959
  • 113
  • 301
  • 329
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 1
    I see. So immutable objects are copied when passed by reference, but mutable objects are not? If this is the case, then why even speak of immutable objects as being passed by reference? If effectively they're passed by value in all relevant circumstances.. – Ben G Dec 24 '11 at 23:58
  • Indeed. To say all variables are references is vastly oversimplifying things, but since there are a small number of mutable types, it makes sense for most cases. – johnsyweb Dec 25 '11 at 09:06
4

You don't pass pointers in Python. Just assign to the slice that is the whole list

def update_list(list, data):
    list[:] = newlist
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

Sure, just make sure to use proper variable names (list is a type):

>>> def update_list(lst):
...   lst.append('hello')
... 
>>> a = []
>>> update_list(a)
>>> a
['hello']
>>>

I'm not a big fan of modifying things in-place and I would actually prefer the second method over the first.

Blender
  • 289,723
  • 53
  • 439
  • 496
1

Aside from taking into account that a list is mutable and that you can modify it in place inside other functions, as has been already pointed out by other answers; if you're writing many update_list methods, you have to consider if the data that is being stored is not really a list but something else that fits into the model you've created as part of the object oriented approach to the problem.

If that's the case, then by all means create your own class and methods to provide the interface that you need to the internal state of your object:

class MyList(list):
    def update(self, data):
        # Whatever you need to update your data

...

my_list = MyList()
my_list.update(data)
jcollado
  • 39,419
  • 8
  • 102
  • 133
0

This StackOverflow question and subsequent accepted response will guide you in the right direction to understanding the Pythonic way of what Python does with values passed in.

In general, the Pythonic way to update the list would be to write/call update_list(list, data).

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230