Python Introducing Lists Using Lists Mutability

Dorota Parzych
Dorota Parzych
5,706 Points

pop() - 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
adrian miranda
13,529 Points

When 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
Dan B
6,151 Points

Good 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.