Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Introducing Lists!
You have completed Introducing Lists!
Preview
Now that we can add stuff, we'll definitely want to make sure we can get stuff out of the list. Let's explore `del` and `pop`.
Code
books = [
"Learning Python: Powerful Object-Oriented Programming - Mark Lutz",
"Automate the Boring Stuff with Python: Practical Programming for Total Beginners - Al Sweigart",
"Python for Data Analysis - Wes McKinney",
"Fluent Python: Clear, Concise, and Effective Programming - Luciano Ramalho",
"Python for Kids: A Playful Introduction To Programming - Jason R. Briggs",
"Hello Web App: Learn How to Build a Web App - Tracy Osborn",
]
Learn More
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right, so I've updated our master
list, it's in the teacher's notes.
0:00
I'm gonna go ahead and copy it, and
paste it, so it's all written now.
0:04
Here we go,
we have the Learning Python book first.
0:09
Let's remind ourselves of
what our app here is doing.
0:12
So I'm going to go to python -i,
for interactive mode,
0:15
and open up wishlist, right.
0:19
It's pulling the first book out,
it's printing the first item out.
0:22
But I've already read that book.
0:25
So I kind of want this off my wish
list before someone buys me an extra
0:27
copy of it.
0:30
I wanna remove it, so you might have
not seen the del statement before.
0:31
Well, basically del which is short for
0:36
delete allows you to essentially
remove a label from an object.
0:38
It deletes the name not the object.
0:44
So let's go ahead and use my delicious
lunch as an analogy one more time.
0:47
So, let's create a new
variable called Craig's lunch.
0:52
And we'll set the value to
the Unicode value of a taco,
0:57
more in the teacher's notes about Unicode.
1:02
So, to do that,
we're gonna use a named Unicode value.
1:05
So, we're gonna say backslash capital N,
and then a curly brace, and
1:08
the name of the Unicode object is taco,
that's what we want there.
1:12
So if you look at this,
you can see a beautiful taco, there it is.
1:17
Now, my taco left overs
are labeled with craigs_lunch,
1:23
but I have a bunch of other
leftovers in the office fridge here.
1:29
So, I also wanna label the type of meat.
1:32
Now, these tacos that I made last night,
they are carne asada.
1:35
So I'm gonna add an additional label.
1:39
So I'm going to say
carne_asada = craigs_lunch and
1:40
we'll see that carne_asada now
also points to that same taco.
1:47
Both of these labels are on that taco,
they are referring to the same object.
1:54
I can remove one of the labels by using
the del keyword, so let's remove my lunch,
2:00
so scary, del craigs_lunch.
2:06
And now you'll see if I type craigs-lunch,
2:10
we get name error because it
doesn't know what's going on.
2:13
It's like, what is craigs_lunch?
2:17
I don't know what it is, cuz I deleted,
I removed the label, now the label's gone.
2:19
But don't fear, my tacos still exist
under my old label of carne_asada.
2:23
That's still around and there is my taco.
2:28
Now, if I remove this label,
2:32
our office manager Molly will come through
and do a quick clean up of the fridge.
2:35
Anything without a label
gets garbage collected and
2:39
it's thrown out,
since no one that's claiming it.
2:42
More in the teachers note about how
garbage collection works in Python.
2:44
List indices work very similar to this,
that is,
2:48
that you can think of them as labels.
2:51
So, I can use the del keyword to
delete an item at a certain index.
2:53
But it is really just deleting the label,
not the actual object.
2:59
Now, for example,
3:03
I definitely don't want this first
book on my personal wish list, right?
3:04
This Learning Python book,
I don't want it on my wish list, but
3:08
I do wanna recommend it still.
3:11
So I'll add a new variable,
called, let's clear this up here.
3:12
So, I'll create a new variable called
recommendation and I'm going to
3:16
go ahead and add a label to
that object of recommendation.
3:21
So now it's got two labels,
it's got book[0] and recommendation,
3:26
and now I can run a del
statement on book[0].
3:31
And if you take a look at books,
you'll see that Learning Python
3:34
is no longer there, but
my variable still has access to it.
3:40
There is the Learning Python
that I've pulled out.
3:45
And sometimes, you don't want
to keep the value around, right?
3:47
You want it to be garbage collected.
3:49
That is when del is a perfect choice,
it's great.
3:51
Often times though,
3:54
you don't want to do a removal from
the list, you wanna keep it in a variable.
3:55
And when this is what you want,
pop is the list method for you.
3:59
By default, if you just use pop,
it takes the last item from the list.
4:03
So if we say books, and
that method is called pop().
4:08
We're gonna pop the last item off there,
so we should get Hello Web App back.
4:12
Now, see how the last item was returned?
4:16
The repl printed it out, right?
4:18
So I could have stored that in a variable.
4:20
This style of removing the last item
is often referred to as LIFO, or
4:23
last in first out, right?
4:27
The last item that's added to the list is
the first one that comes out when you pop.
4:30
Now you can also pop at a specific index.
4:34
So if I wanted to say books.pop(0)
it will pop the first book up,
4:37
so automate the boring stuff and
I look at books it's not there.
4:43
And there we go,
number one in my wish list.
4:47
I think I'm gonna buy this right now,
my wish has been granted.
4:49
Great job getting items added,
inserted, and
4:54
remove from a list. How are things feeling?
4:56
Remember, if anything at all has you
perplexed, you are part of a wonderful
4:59
community of fellow students,
they would love to chat with you.
5:03
You could even pop a question off
that unanswered question list, and
5:06
answer one for your classmates.
5:09
So, now that we can manipulate
these lists, I think it's time
5:11
to start taking a look at some typical
solutions that they provide, sound good?
5:14
Let's talk through some common use
cases of this powerful data type,
5:19
right after this quick break
5:22
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up