Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Lists aren't always perfectly formed. Sometimes you have to take things out. Let's look at the del
keyword and the .remove()
method to learn two different ways of taking out list members.
New Terms
del - A keyword for removing items from iterables or deleting whole variables.
.remove() - A list method that removes items from a list by their value.
-
0:00
Sometimes there are things in your list that just don't need to be there anymore.
-
0:03
You've already seen that just like deleting a normal variable,
-
0:06
you can delete an item from your list so long as you know what the item's index is.
-
0:09
Well what about doing something by value though?
-
0:12
Let's check these two things out in workspaces.
-
0:15
Okay, so let's make ourselves an alphabet list.
-
0:17
So I'll call this alpha_list and it's gonna have strings a, b, z, c and d.
-
0:26
Suppose that could be a zed there in the middle depending on where you're at.
-
0:30
And if I look at the alpha_list and I get all the letters back.
-
0:34
But I don't actually want that z in there, right?
-
0:37
I mean that's not where z goes in the alphabet.
-
0:39
Well, it has an index of 2, since our index counting starts at 0.
-
0:42
And it's the third item, right?
-
0:43
0, 1, 2, 3, 4.
-
0:46
So I could just delete it with del.
-
0:49
So del alpha_list[2].
-
0:51
And if I look at alpha_list, a, b, c, d.
-
0:55
Great, we got rid of that completely unnecessary and out of place z.
-
1:00
We can actually use del on any variable that we want.
-
1:02
One of the great things about learning Python is that you often only have
-
1:05
to learn one technique no matter what kind of data you're working with.
-
1:09
We like things to work the same everywhere.
-
1:11
So what about when you don't know the index of an item or
-
1:15
you don't care to count it out?
-
1:16
Maybe you had one hundred things in the list.
-
1:18
So let's set up a new list with a few things in it.
-
1:20
So my list is 1, 2, 3, 1, okay.
-
1:25
So our list has two 1s in it.
-
1:27
Now the remove method will delete an item by its value.
-
1:31
It effectively looks through a list comparing each item against a value
-
1:33
that you supply.
-
1:35
If the value and
-
1:35
the item match, remove will delete that item and then stop looking.
-
1:40
So let's try and get rid of those 1s, my_list.remove(1).
-
1:45
And my_list is 2, 3, 1, that didn't do what I wanted.
-
1:50
But like I said and I should have listened to myself,
-
1:52
remove removes the first instance of a value from the list.
-
1:55
Since we had more than one 1 in the list, we're left with the second one.
-
1:59
Let's run it again to get rid of the last 1.
-
2:01
So, my_list.remove(1).
-
2:04
And now if I look at my list, it's 2 and 3, awesome, no more 1s in the list.
-
2:09
Now what happens if I use it again though, right?
-
2:13
There's no more 1s.
-
2:14
So, if I run my_list.remove(1)
-
2:16
Python throws a ValueError because that value isn't in there.
-
2:21
Now, that means that we could loop over the list until an exception
-
2:24
happens, right?
-
2:26
Let's use this to add a bit of power to our shopping list.
-
2:29
So let's come up, let's go ahead and
-
2:31
get out of Python actually, and then come up here to our list.
-
2:37
And let's add a little bit of power to this.
-
2:39
So first we need to add it to our instructions right?
-
2:42
DONE, HELP, SHOW, let's add a new one here which
-
2:47
is Enter 'REMOVE' to delete an item from your list.
-
2:54
All right, and then down here,
-
2:58
elif new_item.upper() == 'REMOVE',
-
3:05
then we want to run the function
-
3:09
remove_from_list, all right.
-
3:14
Now, we need to actually write this remove_from_list function.
-
3:20
We have to have the function if we're gonna call it.
-
3:22
So let's add it in right up here.
-
3:28
Def remove_from_list.
-
3:34
Okay?
-
3:35
And I want to remove the first item from the list that has that value so
-
3:41
I'm gonna show_list cuz we need to see the list,
-
3:45
what_to_remove = input("What would you like to remove?.
-
3:51
Do a \n> our normal prompt, space and then that.
-
3:57
And then we're gonna try, let me scroll this up a little bit here, we're gonna
-
4:05
try shopping_list.remove(what_to_remove) except ValueError.
-
4:15
All right.
-
4:19
Then, we're just gonna pass.
-
4:21
We're just gonna quietly skip over that.
-
4:24
And then we should show the list again.
-
4:26
Now, this function is pretty straightforward right?
-
4:29
We show the list, we figure out what they want to remove, we try to remove it.
-
4:33
If it can't be removed, we just pass and then we show the list.
-
4:37
Now we could look and
-
4:38
make sure the thing they want to remove is actually on the list.
-
4:41
This is a little bit cleaner though as far as functionality and Python goes, right?
-
4:48
Cuz if we check the list first, then we have to have this whole step of being
-
4:51
like, well hey, you asked me to remove onions but onions weren't in there.
-
4:55
So I don't know.
-
4:56
You could also send back a message to the user saying
-
4:59
hey onions weren't in your list.
-
5:01
So that's fine, if you want to do that go for it.
-
5:03
All right, but let's see if this works.
-
5:10
So python shopping_list, and I'm gonna add apples, and I'm gonna add oranges.
-
5:17
And just add it to be the list.
-
5:18
Okay, and now I want to remove oranges.
-
5:24
Cool it's gone.
-
5:25
Now I want to remove olives.
-
5:28
I typed in remove olives.
-
5:29
Hah, I need to do remove, and I want to remove olives which aren't on the list.
-
5:36
So it does exactly what we told it to do and it just quits.
-
5:40
So, or not quits it just passes on.
-
5:43
So nice, we got the whole thing working.
-
5:45
Now I want you to use your knowledge of try and
-
5:48
accept blocks, this value error exception, and a bit of looping to build
-
5:51
a script that removes the vowels from whatever words I give it.
-
5:54
It's all explained in the next go challenge.
-
5:56
I know you can do it.
You need to sign up for Treehouse in order to download course files.
Sign up