Python Introducing Lists Meet Lists Deletion

Ceil-Ian Maralit
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,420 Points

Pop vs Del

Hello!

This got me confused a bit. Does pop() and del have the same purpose?

Because Craig said that del is used to just delete the label and not the actual value. But he also said in the latter part that del is great if you want to delete the value and that he recommends pop() when we don't want the items deleted to be garbage collected.

BUT we can still access the values of the items deleted from the list when we assign it first to a variable before executing the command to delete it. Now, what's the difference between del and pop()? and when to use them?

Thanks in advance!

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,993 Points

Good question! A simple difference is pop() returns the item removed so it can be assigned a different label while del simply removes the item.

If you use pop() and do not assign a new label to the returned object it is essentially performing the same function as del.

The list of book title strings is actually a list of references that point to string objects stored in memory. Each string object "id" is the address in memory of the stored object.

book_list = [
   "book title 1",
   "book title 2",
   "book title 3",
   "book title 4"
   ]

print(book_list)
#['book title 1', 'book title 2', 'book title 3', 'book title 4']

print([id(book) for book in book_list])
# [139996358750368, 139996358750256, 139996358750592, 139996358750648]

saved = book_list.pop(0)
print(saved)
# book title 1

print(id(saved))
# 139996358750368

In a more detailed look, pop() returns the "id" reference to the removed object so that it can be assigned to another label or used in a subsequent statement. The label saved is assigned to the first book popped from the book list.

So, use pop() when you want to have access to the removed item for another purpose, and use del when you no longer want the item.

Post back if you have more questions. Good luck!!

Ceil-Ian Maralit
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,420 Points

Hi! Thank you so much for this.

Though I still have a question. So how come we can still access the value deleted using del when we assign it to a variable before deleting it(I tried accessing them before deleting it with pop() and del, which made me really confused), if del means to delete it permanently, as we say it, garbage collected? Does del also provide id reference?

I do understand that it is recommended to use pop() when we want to access it still, after deleting it. Just curious why we can still do it with del. Thank you! :)

Chris Freeman
Chris Freeman
Treehouse Moderator 58,993 Points

Assigning a variable is more like giving a label to an object reference. This increases the “reference count” of an object. Think of del as deleting the label reference to an object rather than deleting the object itself. If all label references to an object are removed (reference count become zero), then garbage collection will eventually remove the object from memory because it has no labels referencing that object.

>>> import ctypes
>>> a = 'some string'
>>> id(a)
4511143024
>>> b = a
>>> id(b)
4511143024
>>> del(a)
>>> ctypes.cast(4511143024, ctypes.py_object).value
some string
>>> del(b)
>>> ctypes.cast(4511143024, ctypes.py_object).value
some string

In the case above, this repl does not have strong garbage so the object can still be referenced by id value even though the last label had been removed. A bit later, the same attempt crashed the repl because garbage collection had removed the object!

Ceil-Ian Maralit
Ceil-Ian Maralit
Front End Web Development Techdegree Graduate 19,420 Points

Ohhh, okay. Now, I see the difference between the two. Though I did not understand the ctypes thing in the code coz I'm just a beginner in Python, but it doesn't matter. It still made sense to me. Thank you so much!

very cool example

Really good questions and answers. Thank you so much!

nikel Hayo
nikel Hayo
1,942 Points

ı was confused and Chris made crystal clear. Thanks a lot for both question and answer.