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

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!"
4 Likes

why that can’t be done with del()
like this:

my_list = [“cat”, “dog”, “mouse”]
deleted = del(my_list[0])

if deleted = “cat”:
print “why”

1 Like

Hi @matanselig,

del is not a function, however you are attempting to utilize it with function call syntax here:

deleted = del(my_list[0])

This, by itself, actually would delete the item represented by my_list[0]:

del(my_list[0])

The parentheses are not needed, so you can do this instead:

del my_list[0]

However since del is not a function, it does not return anything at all, and the line where you used it is not a valid assignment statement.

See the official documentation here: The del statement.

Also see official documentation on the list.pop([i]) method here: Data Structures.

Edited on January 2, 2019 to identify list.pop([i]) as a method.

2 Likes