4/18 explain pop, delete and remove and where does the return go to


m = [100,200,300,400,500,600,700]

print m

print m

print m

[200, 300, 400, 500, 600, 700]
[200, 400, 500, 600, 700]
[200, 400, 500, 600]

So my main problem is what is the difference of each function. what purpose does the return value actually do? thank you


The remove function returns the first instance it can find in the list, as oppose to pop which removes the value at a specific index.

For example, take the list

a = [1,2,4,2,5]

Let's say you use the remove method to get rid of the number 2.


Our list a will now equal [1,4,2,5] because the remove method removed the first (and only the first) 2 in the list. Now lets remove the the value at the second index of list a.

a.pop(1)    # Index 1 represents the second element in the list

Our list a will now equal [1,2,5] because we removed the second element in the list (which was 4).

The pop and delete methods modify the list in the same exact way except pop returns the deleted number and del does not. Sometimes you will want to store the deleted element so this can be very useful. So if we use pop we could do the following:

# a = [1,2,5]
b = a.pop(0)

Now a = [2,5] and b equals 1. If we did the same with the del method:

b = del a[1]   # this causes problems!

this would cause a syntax error because the del method does not return anything. We would have to do the following:

del a[1]

Also check out the Python documentation for further help!