Question:

Consider the following Python declarations of two lists.
\[ A = [1, 2, 3] \quad \text{and} \quad B = [4, 5, 6]. \] Which one of the following statements results in \( A = [1, 2, 3, 4, 5, 6] \)?

Show Hint

In Python, `extend()` adds all elements of one list to another, while `append()` adds the list as a single element.
Updated On: Apr 4, 2025
  • A.extend(B)
  • A.append(B)
  • A.update(B)
  • A.insert(B)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

In Python, the `extend()` method is used to append all the elements of a list to another list. Let's examine each option:

Option (A): `A.extend(B)` will add all elements of list \( B \) to list \( A \), resulting in \( A = [1, 2, 3, 4, 5, 6] \). This is the correct answer.
Option (B): `A.append(B)` will add list \( B \) as a single element to list \( A \), resulting in \( A = [1, 2, 3, [4, 5, 6]] \), which is incorrect.
Option (C): There is no `update()` method for lists in Python, so this is incorrect.
Option (D): `A.insert(B)` is used to insert an element at a specific index, and it cannot be used with an entire list like \( B \). This is also incorrect.

Thus, the correct answer is Option (A), which uses `A.extend(B)`.
Was this answer helpful?
0
0

Top Questions on Programming in Python

View More Questions

Questions Asked in GATE DA exam

View More Questions