Consider the following Python declarations of two lists.
A = [1, 2, 3]
B = [4, 5, 6]
Which one of the following statements results in A = [1, 2, 3, 4, 5, 6]?
A.
A.extend(B)
B.
A.append(B)
C.
A.update(B)
D.
A.insert(B)
Solution:
The question is asking which operation will result in:
A = [1, 2, 3]
B = [4, 5, 6]
# Desired result:
A = [1, 2, 3, 4, 5, 6]
Explanation of options:
(A)
A.extend(B)
✅
This adds each element ofB
to the end ofA
.
→ ✅ Correct. Result:[1, 2, 3, 4, 5, 6]
(B)
A.append(B)
This adds the entire listB
as a single element toA
.
→ Result:[1, 2, 3, [4, 5, 6]]
(C)
A.update(B)
list
objects in Python do not have anupdate()
method.
→ ❌ Invalid — causes anAttributeError
(D)
A.insert(B)
insert()
requires two arguments: index and value.
→ ❌ Invalid — causes aTypeError
✅ Correct Answer: (A) A.extend(B)