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 trialDorota Parzych
5,706 Pointspop() - what was this function exactly doing?
I forgot what the pop() function was doing and why it influences our display_wishlist function Thanks in advance
2 Answers
adrian miranda
13,561 PointsWhen you call pop() on a list, it gives you the last element on the list. However, it also modifies the list, actually removing the last element.
Or if you call pop(0) on a list, it removes the first element (the zeroth element) from the list. So again, it both gives you the first element, and removes it from the list.
In display_wishlist they use pop(0) to remove the first element, and print it with some fancy formatting. Later they go through and print everything in the list, but they don't wind up printing the first element again, because it was removed by the call to pop(0).
Dan B
6,155 PointsGood question, I had the same one. The reason it wasn't used because pop edits the list as the teacher showed. He did this deliberately to show how easy it is to accidentally change a list. You should definitely use
wishes[0]
If you don't want to edit the list like the pop function does.