Identify the correct output of the Python code: L1=[1,2,3] L2=[4,5] L3=[6,7]…
2023
Identify the correct output of the Python code: L1=[1,2,3] L2=[4,5] L3=[6,7] L1.extend(L2) L1.append(L3) print(L1)
- A.
[1, 2, 3, 4, 5, 6, 7]
- B.
[1, 2, 3, [4, 5], 6, 7]
- C.
[1, 2, 3, 4, 5, [6, 7]]
- D.
[1, 2, 3, [4, 5], [6, 7]]
Attempted by 1810 students.
Show answer & explanation
Correct answer: C
Key idea: extend adds elements of the iterable individually to the list; append adds the given object as a single element (which can be a sublist).
Start: L1 = [1, 2, 3].
L2 = [4, 5]. After L1.extend(L2), L1 becomes [1, 2, 3, 4, 5] because extend adds 4 and 5 as separate elements.
L3 = [6, 7]. After L1.append(L3), L3 is appended as a single element, so L1 becomes [1, 2, 3, 4, 5, [6, 7]].
Finally, print(L1) outputs the list with L3 as a nested sublist.
Final output: [1, 2, 3, 4, 5, [6, 7]]