IAE Academy

GATE 2025 DA Q.23: solution and answer

GATE 2025 · DAQ.23 · 1 mark · MCQ

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]?

  1. (A)

    A.extend(B)

  2. (B)

    A.append(B)

  3. (C)

    A.update(B)

  4. (D)

    A.insert(B)

See the question in the paper

Answer and solution

ANSWER

Option (A), A.extend(B), is correct. It is the only statement that leaves A as [1, 2, 3, 4, 5, 6].

Official answer: ASource: Official answer key, GATE 2025 DA (IIT Roorkee)

  1. Start with A=[1,2,3] and B=[4,5,6]. The target is the flat list [1, 2, 3, 4, 5, 6].

  2. (A) A.extend(B) appends every element of the iterable B to the end of A, in place. A becomes [1, 2, 3, 4, 5, 6]. This matches.

  3. (B) A.append(B) adds B as ONE element. A becomes [1, 2, 3, [4, 5, 6]], a nested list of length 4. This does not match.

  4. (C) A.update(B) fails because update is not a list method (it belongs to dict and set). Python raises AttributeError.

  5. (D) A.insert(B) needs two arguments, an index and an object. Called with one argument it raises TypeError.

  6. Only (A) gives the flat concatenation.

KEY IDEA

list.extend(iterable) adds each element of the iterable to the end of the list, in place. list.append(x) adds x as a single element.

WATCH OUT FOR

Choosing A.append(B). It looks like it joins the lists, but it nests B as one element and gives [1, 2, 3, [4, 5, 6]].

From the GATE 2025 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions