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 trial

Python Introducing Lists Using Lists Mutability

David Dong
David Dong
5,593 Points

Why wasn't wishes[0] used instead of wishes.pop(0)?

.pop(0) removes an item from the list, but wishes[0] doesn't. He ended up copying the list, but why not just use wishes[0] instead?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 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 pop(0) vs [0], 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[0]
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!!!