350-401 · Question #945
Refer to the exhibit. What is the value of the variable list after the code is run?
The correct answer is B. [1, 2, 3, 10]. The Python list.insert() method is used to add an element at a specified index in a list, modifying the list in place.
Question
Refer to the exhibit. What is the value of the variable list after the code is run?
Options
- A[1, 2, 10]
- B[1, 2, 3, 10]
- C[1, 2, 10, 4]
- D[1, 10,10,10]
How the community answered
(57 responses)- A16% (9)
- B75% (43)
- C4% (2)
- D5% (3)
Why each option
The Python `list.insert()` method is used to add an element at a specified index in a list, modifying the list in place.
This choice incorrectly suggests the list would truncate or remove elements after insertion.
The `list.insert(index, element)` method inserts an `element` at a specific `index`. For the list `[1, 2, 3, 4]`, inserting `10` at index `2` effectively modifies the list to include `10` after `2` and `3`, resulting in the list `[1, 2, 3, 10]`.
This choice incorrectly suggests that an existing element (3) would be removed while inserting at index 2.
This choice incorrectly suggests multiple insertions or replacements, which is not what `list.insert()` does.
Concept tested: Python list `insert()` method
Source: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
Topics
Community Discussion
No community discussion yet for this question.