What does it mean that pop will return a value to me?


#1

Question

What does it mean that pop will return a value to me?

Answer

pop() is a function, just like the ones you wrote in earlier exercises. The value it returns is the element you remove from the array!
This is very useful when you want to do things like check a value being removed, which we couldn’t do with del() since it doesn’t return a value. If we wanted to store a popped value, we could do so like this:

my_list = ["cat", "dog", "mouse"]
popped = my_list.pop(0)

if popped == "cat":
  print "We found the cat!"

FAQ: Learn Python - Lists and Functions - Removing elements from lists