Python Arena
Step 2 of 4

Copying a list

When you want a second list that you can change on its own, you have to make a copy. The long way is a new empty list and a loop that appends each item to it:

original = [3, 1, 2]
duplicate = []
for item in original:
    duplicate.append(item)

duplicate.append(4)
print(original)
print(duplicate)
Output
[3, 1, 2]
[3, 1, 2, 4]

Now there are two lists, and changing one leaves the other alone. Python has three shorter ways to do the same, and each of them gives a new list with the same items:

In the Python interpreter
>>> original = [3, 1, 2]
>>> a = original.copy()
>>> b = original[:]
>>> c = list(original)
>>> a.append(4)
>>> b[0] = 99
>>> c.sort()
>>> original
[3, 1, 2]
>>> a
[3, 1, 2, 4]
>>> b
[99, 1, 2]
>>> c
[1, 2, 3]
  • copy() is a list method that does just that.
  • [:] is a slice with no start and no end, so it takes every item. A slice is always a new list.
  • list(...) builds a new list from the items it's given, as it does with range.

All three work. copy() is the easiest to read, because it says what it does.

Copying a list of lists

A copy holds the same items as the original. When those items are lists, as in a matrix, the copy holds references to the very same inner lists. The two outer lists are separate, but they share their rows:

board = [[".", "."], [".", "."]]
saved = board.copy()

board[0][0] = "X"
board.append(["new", "row"])
print(saved)
Output
[['X', '.'], ['.', '.']]

The new row went into board only, because the outer lists are separate. But the "X" went into a row that both lists share, so it shows up in saved too. To copy a matrix so that nothing is shared, copy each row as well:

board = [[".", "."], [".", "."]]
saved = []
for row in board:
    saved.append(row.copy())

board[0][0] = "X"
print(board)
print(saved)
Output
[['X', '.'], ['.', '.']]
[['.', '.'], ['.', '.']]

Your task

Challenge 1 of 3

The sale prices should be half of the normal prices, but the normal prices must stay as they are. Fill in the blank with a copy of prices, so the program prints:

Normal: [10, 25, 40]
Sale: [5, 12, 20]

Challenge 2

What does this program print? Type each line it prints.

Challenge 3

Write a function copy_grid(grid) that returns a copy of a matrix. Nothing may be shared between the copy and the original: after the copy is made, changes to the original's rows must not show up in the copy.

grid = [[1, 2], [3, 4]]
saved = copy_grid(grid)
grid[0][0] = 99
print(saved)
print(grid)
[[1, 2], [3, 4]]
[[99, 2], [3, 4]]