350-401 · Question #551
Refer to the exhibit. What is the value of the variable list after the code is run?
The correct answer is C. [1, 2, 1, 2, 1, 2]. In Python, multiplying a list by an integer results in a new list that contains the elements of the original list repeated and concatenated consecutively.
Question
Refer to the exhibit. What is the value of the variable list after the code is run?
Options
- A[1, 2], [1, 2], [1, 2]
- B[1, 2] * 3
- C[1, 2, 1, 2, 1, 2]
- D[3, 6]
How the community answered
(34 responses)- A15% (5)
- B3% (1)
- C74% (25)
- D9% (3)
Why each option
In Python, multiplying a list by an integer results in a new list that contains the elements of the original list repeated and concatenated consecutively.
This syntax represents a tuple of lists or a list containing separate list objects, which is not the behavior of list multiplication; list multiplication creates a single, flattened list.
This is the expression itself, not the evaluated result that would be assigned to the `list` variable after the code executes.
When a Python list is multiplied by an integer (e.g., `[1, 2] * 3`), the list's elements are repeated that many times and concatenated into a single, flat list, resulting in `[1, 2, 1, 2, 1, 2]`.
This would be the result of element-wise multiplication, which is not performed by the `*` operator when multiplying a list by an integer in Python.
Concept tested: Python list multiplication behavior
Source: https://docs.python.org/3/tutorial/introduction.html#lists
Topics
Community Discussion
No community discussion yet for this question.