1

Question regarding list in python: I have one program in that list are generating and assigning to a different variable but still getting the same output for two variable...Not able to understand the functionality.

Code:

def extendList (val, list=[]):
    list.append(val)
    return list
list1 = extendList (10)
list2 = extendList (123, [])
list3 = extendList ('a')

print list1, list2, list3
  • hard to say without seeing an expected and actual output, but I suspect your may be getting tripped up by pythons mutable default arguments. See https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument – Zinki Aug 16 '18 at 15:07
  • Here's another link that I think does a great job: [click here](http://effbot.org/zone/default-values.htm) – Rushabh Mehta Aug 16 '18 at 15:08
  • 1
    System output is: [10, 'a'] [123] [10, 'a'] And my output is: [10], [123], [10,'a'] – Pooja Rastogi Aug 16 '18 at 15:08
  • 1
    @PoojaRastogi Read the link I've shared. It explains this behavior – Rushabh Mehta Aug 16 '18 at 15:09
  • Hi Rushabha, Thanks for the link.. I have read the article but my situation is different because in my I am using different variables to save the values. For 1st variable list1 storing 10 into it but system output is [10, 'a'] at last. if we separately put print command after list1 it will give output 10 only – Pooja Rastogi Aug 16 '18 at 15:16
  • Please look to below code and corresponding output. And see the difference in system output: def extentList (val, list=[]): list.append(val) return list list1 = extentList (10) print list1 list2 = extentList (123, []) list3 = extentList ('a') print list3 print list1, list2, list3 System Output: C:\Users\rastogip\Documents\Python>excersice1.py [10] [10, 'a'] [10, 'a'] [123] [10, 'a'] – Pooja Rastogi Aug 16 '18 at 15:17

2 Answers2

0

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']
MarianD
  • 13,096
  • 12
  • 42
  • 54
0

That happens because of the default list value. The list value remains the same at each calll of your function

"Python’s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well."

from here

Lior
  • 84
  • 3