Python Introducing Lists Using Lists Mutability

Jesse Davidson
Jesse Davidson
647 Points

.copy method issue

Hi guys,

I understand that the .copy method makes copy of the existing list as this will prevent you from making changes to the existing list.

However, What I do not understand in the course material is that when he assigns the .pop method to the items then it does not remove the the first index (items.copy(0)) in the copied list. Instead, it stays there.

Example:

ef display_wishlist(display_name,wishes): items = wishes.copy() print(display_name + ":") suggested_gift = items.pop(0) print("=====>",suggested_gift,"<=====") for item in items: print("* " + item) print()

display_wishlist("Books",books)
display_wishlist("Video games",video_games)
display_wishlist("Video games",video_games)

The printed out display_wishlist function was written out twice, but why does this not remove the first item in the copied list like it did in the existing list when the .copy method was not implemented.

1 Answer

Majid Bilal
Majid Bilal
3,558 Points

Because Items get renewed every time the function is called,

items = wishes.copy()

this line refreshes items as we are reassigning it the copy of wishes.