What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)

Python Developer Medium

Python Developer — Medium

What is the output of the following code? x = [1, 2, 3] y = x y.append(4) print(x)

Key points

  • Assigning a list to another variable creates a reference, not a copy
  • Modifying one variable affects the other due to shared memory
  • Appending to y also appends to x in this scenario

Ready to go further?

Related questions