Inner workings of lists
Some code has been provided for you in this exercise: a list with the name areas
and a copy named areas_copy
.
Currently, the first element in the areas_copy
list is changed and the areas
list is printed out. If you hit the run code button you'll see that, although you've changed areas_copy
, the change also takes effect in the areas
list. That's because areas
and areas_copy
point to the same list.
If you want to prevent changes in areas_copy
from also taking effect in areas
, you'll have to do a more explicit copy of the areas
list with list()
or by using [:]
.
This exercise is part of the course
Introduction to Python
Exercise instructions
- Change the second command, that creates the variable
areas_copy
, such thatareas_copy
is an explicit copy ofareas
. After your edit, changes made toareas_copy
shouldn't affectareas
. Submit the answer to check this.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create list areas
areas = [11.25, 18.0, 20.0, 10.75, 9.50]
# Change this command
areas_copy = areas
# Change areas_copy
areas_copy[0] = 5.0
# Print areas
print(areas)