Skip to content
Programs · Program 9-2

Q.Program 9-2: Program to increment the elements of the list passed as parameter.

Uttarakhand UbseTextbookSubjective· 3mImportance★★★★★est
15% · 4/26 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Program 9-2 shows the flip side of Program 9-1: when the function assigns a new list to the parameter name (instead of changing elements in place), the caller's list is untouched — after the call, list1 is still [10, 20, 30, 40, 50].

The idea (rebinding vs mutating). A parameter is a name. list2[i] = value mutates the shared object (Program 9-1), but list2 = [ … ] merely makes the local name point at a different, newly created list. The original object still has the caller's name list1 bound to it, so outside the function nothing has changed. Watching id() makes this visible: inside the function the id changes after the assignment.

Program

def increment(list2):
    print("ID of list inside function before assignment:", id(list2))
    list2 = [15, 25, 35, 45, 55]      # a NEW list object is created
    print("ID of list inside function after assignment :", id(list2))
    print("List inside function :", list2)

list1 = [10, 20, 30, 40, 50]
print("ID of list before function call:", id(list1))
print("List before function call:", list1)
increment(list1)
print("ID of list after function call :", id(list1))
print("List after function call :", list1)

Expected output (id numbers vary per run; what matters is the PATTERN — the id inside changes after the assignment, and the caller's id is unchanged):

ID of list before function call: 140489685176384
List before function call: [10, 20, 30, 40, 50]
ID of list inside function before assignment: 140489685176384
ID of list inside function after assignment : 140489685177472
List inside function : [15, 25, 35, 45, 55]
ID of list after function call : 140489685176384
List after function call : [10, 20, 30, 40, 50]

What happened, step by step

| Step | Name and object |

| --- | --- | …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.