CBSE Class 12 Computer Science Question 56 of 68

Data Structures - I : Linear Lists — Question 12

Back to all questions
12
Question

Question 12(i)

Predict the output.

b = [[9, 6], [4, 5], [7, 7]]
x = b[:2] 
x.append(10)
print(x)
Answer
Output
[[9, 6], [4, 5], 10]
Explanation
  1. b = [[9, 6], [4, 5], [7, 7]] — This line initializes a list b containing three sublists, each containing two elements.
  2. x = b[:2] — This line creates a new list x by slicing the list b from index 0 to index 1. So, x will contain the first two sublists of b. At this point, x will be [[9, 6], [4, 5]].
  3. x.append(10) — This line appends the integer 10 to the end of the list x. x now becomes [[9, 6], [4, 5], 10].
  4. print(x) — This line prints the list x.