Python · Question 27 · Hard
What will be the output of the following Python code?
a = [[0] * 3] * 2
a[0][1] = 5
print(a)
- [[0, 5, 0], [0, 5, 0]]
- [[0, 0, 0], [0, 5, 0]]
- [[0, 5, 0], [0, 0, 0]]
- [[5, 5, 5], [0, 0, 0]]
Answer and explanation
Answer: A. [[0, 5, 0], [0, 5, 0]]
Explanation: Multiplying the outer list copies the reference, so both rows are the same list object. Changing a[0][1] therefore shows up in both rows.
Previous: What will be the output of the following Python code? · Next: What will be the output of the following Python code?
Practise python
Try a fresh set of python questions with an explanation after every answer, free and without signing up: Python practice. Or sit a full placement mock test.
More python questions
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
- What will be the output of the following Python code?
More technical & programming practice
Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge