I recommend reading the semantics of Python variable names from a C ++ perspective :
All variables are references.
This is a simplification of the entire article, but this (and understanding that list is volatile ) 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 with the extend () method:
In [9]: def update_list(lst, data): ...: lst.extend(data) ...:
In fact, the need for your function is probably eliminated.
NB: list is inline and therefore a poor choice for a variable name.
source share