A=[1,2,3] B=[4,5,6]
Which one of the following statements results in ?
A
Step-by-Step Solution
Insight: The question tests the difference between adding an iterable as a single nested object versus flattening its elements into the existing list.
Exam route: Eliminate options that use non-existent list methods or wrong argument counts. Between the remaining two, recall that extend flattens while append nests.
Learning route:
- A.extend(B): The extend method iterates over the argument B and adds each element (4, 5, 6) one by one to A. The final list is [1, 2, 3, 4, 5, 6]. This matches the requirement.
- A.append(B): The append method adds the entire object B as a single element at the end of A. The final list becomes [1, 2, 3, [4, 5, 6]]. This does not match.
- A.update(B): The update method is not defined for Python lists (it is used for sets and dictionaries). Calling this raises an AttributeError.
- A.insert(B): The insert method requires exactly two arguments: an index and an element. Providing only one argument raises a TypeError.
Therefore, A.extend(B) is the only statement that produces the desired flat list.