CBSE Class 12 Computer Science Question 11 of 19

Data Structures - I : Linear Lists — Question 10

Back to all questions
10
Question

Question 10

Predict the output :

L1= [x ** 2 for x in range(10) if x % 3 == 0]
L2 = L1
L1.append(len(L1))       
print(L1)
print(L2)      
L2.remove(len(L2) - 1) 
print(L1)
Answer
Output
[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
Explanation
  1. L1 = [x ** 2 for x in range(10) if x % 3 == 0] — This line creates a list L1 containing the squares of multiples of 3 between 0 to 9.
  2. L2 = L1 — This line assigns list L1 to list L2, meaning both L1 and L2 reference the same list in memory (shallow copy).
  3. L1.append(len(L1)) — This line appends the length of L1 to L1. So, the length of L1 is 4, and it appends 4 to L1.
  4. print(L1) — This prints the modified L1 list.
  5. print(L2) — This prints L2, which is pointing to the same list as L1.
  6. L2.remove(len(L2) - 1) — This line removes the last element of L2 since len(L2) - 1 gives the last index of L2.
  7. print(L1) — This prints L1 again. Since both L1 and L2 reference the same list, modifying L2 also affects L1. Therefore, L1 also reflects the change made to L2.
Get the Bright Tutorials app Stuck on a question? Ask Bright Buddy — your AI tutor — for step-by-step help in the app.