In the command
def extentList (val, list=[]):
the list variable is initialized only once - after the first calling it remains as it is.
Replace your code with
def extentList (val, list=None):
if list is None:
list = []
list.append(val)
return list
See Default parameter values are evaluated when the function definition is executed in the Python Tutorial:
... if the function modifies the object (e.g. by appending an item to a list),
the default value is in effect modified...
Put print list1
, immediately after list1 = ...
and print list2
immediately after list2 = ...
to see that list1
and list3
are two different names for the same list - you used the default parameter for creating list1
and list3
, and this default parameter - the list - is initialized (i. e. created, too) only once:
list1 = extendList (10)
print list1
list2 = extendList (123, [])
print list2
list3 = extendList ('a')
print list1, list2, list3
The output:
[10]
[123]
[10, 'a'] [123] [10, 'a']