Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJesse 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
3,558 PointsBecause 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.