Nazmus Salehin Nazmus Salehin - 1 month ago 17
Python Question

Python (parameter passing)

When passing a list as an argument in function why the following list is not changed ?

def foo(*x):
y=sorted(x)
print(y)

a=[3,2,1]


The function is returning
[[3, 2, 1]]
. Not
[[1,2,3]]

Why is this happening ? Is this something to do with call by value ?

Answer

Why creating another function? You can just do this

>>> a = [3,2,1]
>>> sorted(a)
[1, 2, 3]

However if you want to create another function. You have to call it.

def foo(x):
    y=sorted(x)
    return y
a = [3,2,1]
print(foo(a))