Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Meet Lists Deletion

Difference between del and pop

In the video at 3:54 I had a misunderstanding f what del really was.

This is because it was mentioned that del can be used when you don twant the value around and u want it to be garbage collected. However the value is still around due to such variable having the ability to store the value. As usin del only removes the label and not the actual value. Unlike pop

This might be my misunderstanding hopefully it can be cleared

3 Answers

Jason Wiram
Jason Wiram
42,762 Points

The previous answer is not entirely correct. del removes an item at a specific index

> yourArray = [3, 2, 2, 1]
> del yourArray[1]
[3, 2, 1]

pop removes an item at a specific index (and returns it)

> yourArray = [4, 3, 5]
> yourArray.pop(1)
3
> yourArray
[4, 5]

This was very helpful!

.pop seems to have "deleted" 3 in your example. yourArray no longer holds .pop(1), were is .pop(1) stored, and how can I access it, or is it gone? and why would I want to use del instead of manually removing the item from the list, in what scenario would I ever use del in?

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

Right! this is a good starting point for me to understand. now i need to know what happens to the items after they are popped . thank you for making it REAL simple :)

If pop is a method to the object list, e.g., books.pop(), what is the classification of del? Is it also a method? If so, how come it is done or written in a different way than all the methods we have learnt thus far?

Thanks.

I believe, as Craig said, del is simply a statement, not a method. This statement remove a certain label from an object. If this object doesn't get a new label before the del statement is used, it will be garbage collected, since you can no longer access it.

Aaron Hinesley
Aaron Hinesley
2,993 Points

Here is what I used to help me know the difference. If you use delete, you have to write more code to keep the value: example1 = "Hello"

example2 = example1

del example1

So now the string is saved with a new label. But there must be an easier way to do this right? That's when pop becomes the better way to do this.

example1 = ["Hello"]

example2 = books.pop()

Now the string is removed from the list and already has a new label with less script! I hope this helps in any way.

What happens when you simply run books.pop(1) without assigning it to a variable? Is books(1) completely removed in that case or is it stored somewhere? If the latter, where is it stored?

Mark Nembhard
Mark Nembhard
1,387 Points

Like it. But then why have Del on its own when you have pop that can manage both tasks

Aaron Hinesley
Aaron Hinesley
2,993 Points

Good question. I would think Del will come in handy if you do not want to keep the specific data around any longer. If you have duplicates of data and want to start condensing. It would be best to just delete rather than pop it out with no intentions of using it. It offers flexibility on how you want the program to handle the task. Rather than there being only one way to deal with it.