Find the output of the following: def UpdateList(Val, Collection=[]):…
2026
Find the output of the following:
def UpdateList(Val, Collection=[]):
Collection.append(Val)
return Collection
L1 = UpdateList(10)
L2 = UpdateList(20, [1, 2])
L3 = UpdateList(30)
print("List 1:", L1)
print("List 2:", L2)
print("List 3:", L3)
print("Identity Check:", L1 is L3)
Show answer & explanation
Function
UpdateList(Val, Collection=[])uses a mutable default argument (list), which is shared across calls.L1 = UpdateList(10)⇒ default list becomes[10]L2 = UpdateList(20, [1, 2])⇒ new list is used ⇒[1, 2, 20](no effect on default list)L3 = UpdateList(30)⇒ same default list is reused ⇒[10] → [10, 30]Outputs:
List 1: [10, 30]
List 2: [1, 2, 20]
List 3: [10, 30]
L1 is L3⇒ True (both refer to the same default list object)