Robotics & Artificial Intelligence

Give the output of the following Python code.

my_list = [1, 2, 3, 4]
my_list.append(5)
my_list.insert(2, 10)
print(my_list)
  1. [1, 2, 3, 4, 5, 10]
  2. [1, 10, 2, 3, 4, 5]
  3. [1, 2, 10, 3, 4, 5]
  4. [1, 2, 3, 10, 4, 5]

Python List Manipulation

1 Like

Answer

[1, 2, 10, 3, 4, 5]

Reason — First, append(5) adds 5 at the end of the list, so the list becomes [1, 2, 3, 4, 5]. Then, insert(2, 10) inserts 10 at index position 2, shifting the existing elements to the right. Hence, the final list printed is [1, 2, 10, 3, 4, 5].

Answered By

3 Likes


Related Questions