1 1 vote Consider the following Python code:grid = [[0] * 2] * 3 grid[0][1] = 5 grid[2] = [7, 8] grid[1][0] = 9 print(grid)What is the output of the code above?[[0, 5], [9, 5], [7, 8]][[9, 5], [9, 5], [7, 8]][[9, 5], [9, 5], [9, 5]][[0, 0], [0, 0], [7, 8]] Programming in Python goclasses goclasses-da-dpp goclasses-da-dpp-day-192 programming-in-python goclasses-python-&-dsa-practice-questions output + – GO Classes 97 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
1 1 vote The expression $\texttt{[[0] * 2] * 3}$ creates $3$ references to the same inner list.So after $\texttt{grid[0][1] = 5}$, the shared inner list becomes:[0, 5]At this point, all $3$ rows appear as $\texttt{[0, 5]}$ because they refer to the same list.Then $\texttt{grid[2] = [7, 8]}$ replaces only the third row with a new separate list.Now $\texttt{grid[0]}$ and $\texttt{grid[1]}$ still share the same list, but $\texttt{grid[2]}$ is separate.So $\texttt{grid[1][0] = 9}$ changes both the first and second rows.Therefore, the output is:[[9, 5], [9, 5], [7, 8]]Correct Option: B GO Classes answered Jun 5 GO Classes comment Share Follow 0 reply Please log in or register to add a comment.