What Is The Output Of The Following Python List Code Fragment? Justify Your Answer.

Code:

def extendList(val, list=[]):
    list.append(val)
    return list

list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')

print("list1 = ", list1)
print("list2 = ", list2)
print("list3 = ", list3)

Output

list1 =  [10, 'a']
list2 =  [123]
list3 =  [10, 'a']

Explanation: The flow is like that a new list gets created once after the function is defined. And the same get used whenever someone calls the extendList method without a list argument. The list1 and list3 are operating on the same default list, whereas list2 is running on a separate object that it has created on its own (by passing an empty list as the value of the list parameter).