What is the output of the following Python code snippet?
a = [1, 2, 3]
b = a.copy()
b[0] = 99
print(a, b)
b = a.copy()
b[0] = 99
print(a, b)
Correct answer
C. [1, 2, 3] [99, 2, 3]
C. [1, 2, 3] [99, 2, 3]
a.copy() creates a shallow copy, so changing the copy leaves the original unchanged.
Asked in: Python Interview Lists
https://docs.python.org/3/tutorial/datastructures.htmlNot sure why the answer is C? Ask here and get an explanation.