Python Introducing Lists Using Lists Mutability

Dorota Parzych
Dorota Parzych
2,376 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

1 Answer

adrian miranda
adrian miranda
12,217 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).