+ 1
lists and function
how can i define a function in which x can belong to a list?
1 Respuesta
+ 4
In Python you can access a list from inside the function via its methods even if it's defined in the global sphere.
def f(x):
numbers.append(x)
numbers = [1, 2, 3]
f(5)
leads to numbers being [1, 2, 3, 5].
(Yes, this function would be utterly useless. ^^ But anyway, you can access that list.)
You can also pass the list as an argument and again use the methods to change it, because the 'copy' of that list you're using is actually only another name for that very list.
I'm not sure this was what you wanted to know...