Skip to content
Programs · Program 9-1

Q.Program 9-1: Program to increment the elements of a list. The list is passed as an argument to a function.

Uttarakhand UbseTextbookSubjective· 3mImportance★★★★★est
8% · 2/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-1 passes a list to a function that adds 5 to every element in place; since lists are mutable and passed by reference, the change is visible in the caller — [10, 20, 30, 40, 50] becomes [15, 25, 35, 45, 55].

The idea (mutating a list parameter). When a list is passed as an argument, the parameter name becomes another reference to the same list object — no copy is made. So an element assignment such as list2[i] += 5 inside the function updates the very list the caller owns. Printing id() before, inside and after the call proves it is one object throughout.

Program

def increment(list2):
    for i in range(0, len(list2)):
        list2[i] += 5
    print("Reference of list inside function :", id(list2))

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

Expected output (the exact id number varies from run to run — the point is that all three are EQUAL):

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

Trace of the loop (one row per iteration):

| i | list2[i] before | list2[i] after | List state |

| --- | --- | --- | --- | …

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.