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!
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 trial
David Dong5,593 Points
Why wasn't wishes used instead of wishes.pop(0)?
.pop(0) removes an item from the list, but wishes doesn't. He ended up copying the list, but why not just use wishes instead?
Chris FreemanTreehouse Moderator 68,390 Points
Good question! Since the parameter
wishes is mutable (changeable) list, by using
.pop() it modifies the contents of the list seen outside of the function call. This “side effect” might not be desirable. My making a copy, this prevents changing the list seen outside of the function.
As for why
, the desire is to suggest the first item in the list then display the remainder of the list. This could also have been accomplished without using the copy:
suggested_gift = wishes for wish in wishes[1:]:
by using the
wishes[1:] slice, a copy is automatically created.
Post back if you need more help. Good luck!!!