It's because you assigned mylist to some other object in function 2:
In python if you pass a mutable object to a function then it is called passed
by reference and if you pass a immutable object then it is called pass by value.
mylist = [1,2,3,4]
def changeme( mylist ):
print (id(mylist)) #prints 180902348
#at this point the local variable mylist points to [10,20,30]
mylist = [1,2,3,4]; #this reassignment changes the local mylist,
#now local mylist points to [1,2,3,4], while global mylist still points to [10,20,30]
print (id(mylist)) #prints 180938508 #it means both are different objects now
print ("Values inside the function: ", mylist)
return
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
first one:
def changeme( mylist ):
print (id(mylist)) #prints 180902348
mylist.append([1,2,3,4]); #by this it mean [10,20,30].append([1,2,3,4])
#mylist is just a label pointing to [10,20,30]
print (id(mylist)) #prints 180902348 , both point to same object
print ("Values inside the function: ", mylist)
return
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)