The solution is the code below, but the best way is always to return a value.
a = []
def foo():
b = [1,2,3]
a.extend(b[1:])
foo()
# a = [1, 2, 3]
Explanation: You are creating a new list in the scope of the function instead, but by using built in function like append, the list will be modified as python instead uses the one defined in global scope. This is because in python variables are not declared through the use of key words like for example javascript:
let x = 3; // variable x is created
x = 4; // variable x is redeclared
they are declared automatically this causes python to create a new variable in the scope of the function called list:
a = []
def foo():
a = [1, 2, 3] # creates list in the scope of this function
foo()
# a = []
But if we instead call methods on the list "a" in the function it will instead use the list defined in the global scope, like this:
a = []
def foo():
a.append(1) # uses list defined in global scope
a.append(2)
a.append(3)
foo()
# a = [1, 2, 3]