Del vs pop?


#1



Lists and Functions


I'm confused at why pop and del seem to do the same thing. I thought del wasn't supposed to return the item. I would have expected to see the list printed as [1, 3, 5] with the code:


Replace this line with your code. 
n = [1, 3, 5]
# Remove the first item in the list here
del(n[0])
print n


#2

list.pop() can be assigned to a variable after it is removed from the list.

n = [1, 2, 3]
p = n[pop()]
print p        # 3

del is a function more intended for deleting variables, though as we see, it can be used for deleting list elements.

The built-in del function does not belong to any class of objects so can be called on anything. The pop method is only available to objects of the list class.

Please post a link to the lesson when asking questions in the forums. That way we can go and see what it is about and make our answer somewhat jive with the lesson text.


#3

https://www.codecademy.com/en/courses/python-beginner-nzzVa/0/4?curriculum_id=4f89dab3d788890003000096


#4

Some comparisons. Sorry for the long list but it seemed like a good plan...

>>> u = list('83275-93753289475-32874-598')
>>> del(u[23])
>>> u
['8', '3', '2', '7', '5', '-', '9', '3', '7', '5', '3', '2', '8', '9', '4', '7', '5', '-', '3', '2', '8', '7', '4', '5', '9', '8']
>>> v = u.pop(17)
>>> v
'-'
>>> u
['8', '3', '2', '7', '5', '-', '9', '3', '7', '5', '3', '2', '8', '9', '4', '7', '5', '3', '2', '8', '7', '4', '5', '9', '8']
>>> u.remove('-')
>>> u
['8', '3', '2', '7', '5', '9', '3', '7', '5', '3', '2', '8', '9', '4', '7', '5', '3', '2', '8', '7', '4', '5', '9', '8']
>>> del(u[-6])
>>> u
['8', '3', '2', '7', '5', '9', '3', '7', '5', '3', '2', '8', '9', '4', '7', '5', '3', '2', '7', '4', '5', '9', '8']
>>> v = u.pop(-12)
>>> v
'8'
>>> u.pop()
'8'
>>> u
['8', '3', '2', '7', '5', '9', '3', '7', '5', '3', '2', '9', '4', '7', '5', '3', '2', '7', '4', '5', '9']
>>> u.remove('8')
>>> u
['3', '2', '7', '5', '9', '3', '7', '5', '3', '2', '9', '4', '7', '5', '3', '2', '7', '4', '5', '9']

In the first set of commands all the dashes are removed, using each method and the function. In the second set we demonstrate the use of negative index to count from the right side, starting at -1 We also pop with no index which is not allowed with []'s. The pop function has a default, a list parser does not. At the end of it all, the 8's are all gone.

Cool stuff once it is understood.

A common mistake we see is learners writing loops that iterate over a list and they arbitrarily del, pop, or seemingly remove from the same list they are iterating, shortening the list each time. This results in skipped values.

Hey look,words!

We want to take out all the 'o's.

>>> s = list("Hey look, words!")
>>> for i in s:
    if i == 'o':
        s.remove(i)
        print (s)

        
['H', 'e', 'y', ' ', 'l', 'o', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
['H', 'e', 'y', ' ', 'l', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
>>>

Of course the cure is simple, use two lists, one the iterable, the other the mutable.

>>> s = list("Hey look, words!")
>>> for i in list(s):
    if i == 'o':
        s.remove(i)
        print (s)

        
['H', 'e', 'y', ' ', 'l', 'o', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
['H', 'e', 'y', ' ', 'l', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
['H', 'e', 'y', ' ', 'l', 'k', ',', ' ', 'w', 'r', 'd', 's', '!']

Which ever method or function we choose to reach for, the above approach works the same.

A slice copy works as well...

>>> s = list("Hey look, words!")
>>> for i in s[:]:
    if i == 'o':
        s.remove(i)
        print (s)

        
['H', 'e', 'y', ' ', 'l', 'o', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
['H', 'e', 'y', ' ', 'l', 'k', ',', ' ', 'w', 'o', 'r', 'd', 's', '!']
['H', 'e', 'y', ' ', 'l', 'k', ',', ' ', 'w', 'r', 'd', 's', '!']
>>>

Either way, the iterable is static, while the objective is mutable, and otherwise not in the logic. It is the target object. The iterable always knows what it looked like in the beginning, and which is the next value to examine.

Now for the words of caution.

What will happen if our program contains this line?

s = s.remove('-')

What will happen if our program contains this line?

del(s)

Don't look until you find out by testing each line.


The first eg. with return `None`

>>> s = s.remove('!')
>>> s
>>> print (s)
None
>>>

The second wil delete the variable.

>>> del(s)
>>> s
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    s
NameError: name 's' is not defined
>>>

list.pop() is not such a bad way to go sometimes, especially if you keep a copy of the movst recent value, or an undo list. Off topic.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.