Hello Everyon.,
In the Boredless Tourist Project, Instruction 32
32.Append the attraction
passed into add_attraction
to the list attractions_for_destination
.
That’s all we want this function to do, so we can return
after adding the attraction to the list.
I wonder how/why to append a new item in a variable containing a sublist of the main list also add the new item in the main list, the code is as follows:
def add_attraction(destination, attraction):
destination_index=get_destination_index(destination)
attractions_for_the_destination=attractions[destination_index]
attractions_for_the_destination.append(attraction)
return attractions_for_the_destination
How is that the following code append a new attraction in both lists, “the attractions_for_the_destination” and the main list “attractions”
attractions_for_the_destination.append(attraction)
the append is over attractions_for_the_destination, I would think the attractions list wouldn’t be changed but it is, when I print attractions_for_the_destination and attractions both have the new item
I tested it with other lists and happened the same, append a new item in a variable containing a sublist of a main list adde the new item in both
Could someone explain the logic behind it?
Thanks everyone