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
Let's iterate through each value in your list.
Code
video_games = [
"The Legend of Zelda: Breath of the Wild",
"Splatoon 2",
"Super Mario Odyssey",
]
-
0:00
[MUSIC]
-
0:04
One of the most common things to do with lists is to loop or
-
0:07
iterate through every item.
-
0:09
Now, typically we iterate through each value and
-
0:12
then run some code against that item.
-
0:14
Now, the most common way to do this is with a for-in loop.
-
0:18
We can use the for-in loop because the list is iterable.
-
0:22
That is, it's able to be iterated.
-
0:24
Let's take a look at this ability in the REBEL.
-
0:28
Okay, so let's take a look at this meeting.py file in interactive mode.
-
0:33
So we'll kick this off, python -i meeting.py.
-
0:39
And we have an attendees list.
-
0:41
Remember?
-
0:43
So we have our attendees here.
-
0:44
And this is something we can iterate over.
-
0:46
And the syntax is pretty clean.
-
0:49
It's four and in our case the thing is attendee.
-
0:53
And this will be the variable that our item is stored in for each iteration.
-
0:58
And then the key word, in, and then our list, which is again, attendees.
-
1:04
And then we need to do a colon to start the block.
-
1:07
And then we can work with each one of these items cuz they're already
-
1:10
stored in the attendee variable.
-
1:12
So, let's just print it out.
-
1:13
So I'll say print(attendee).
-
1:18
Awesome.
-
1:20
So, I just want you to imagine that each time through the loop that this variable,
-
1:24
this attendee here is set to the next item from here.
-
1:27
So it just goes one through each right?
-
1:29
So it goes, Ken, Alena, Trevor, Ashley, James.
-
1:31
And just sets that attendee each time through the loop.
-
1:34
Now, here's something that you might not expect.
-
1:37
The last value is actually still available with that label
-
1:40
even after the loop is done.
-
1:42
So if I just type attendee here,
-
1:43
you'll see that's set to the last thing we looped through.
-
1:47
So attendee is still set.
-
1:48
I'm not sure you'll ever need that, but you never know.
-
1:51
One more think while we're here in the REBEL, the end keyword.
-
1:55
Just like how you can use it to see if a string is inside another string, or
-
1:59
we can also use it to see if one item is in the list like for instance.
-
2:03
I can see if actually Ashley was in our list.
-
2:04
I will just say hey, is Ashley in attendees?
-
2:10
True she is, awesome.
-
2:12
So let's pop out of this and let's switch over.
-
2:15
To our wish list file.
-
2:20
So I was thinking how about we make this show every one of the items in our
-
2:24
wish list.
-
2:26
Let's just do the entire list.
-
2:27
So, let's see, so we'll say, let's make a little header here.
-
2:30
So we'll do ("Books:"), and we'll say for book, that's our thing,
-
2:36
in books:, so that's gonna loop through each one of this will be set.
-
2:41
That'll be the book, that'll be the book, that'll be that book, right?
-
2:46
And so, I wanna make sure that this is on 4, space is 4 here.
-
2:51
Don't forget that.
-
2:53
Here we go.
-
2:54
For booking book,so now we want to print them.
-
2:57
And let's do a little bullet, we will do a little asterisks there.
-
3:01
You could use some Unicode magic if you wanted to.
-
3:05
So we'll look through each one of these and then print out the books, cool.
-
3:09
Let's go ahead, let's save that and then run it.
-
3:12
So python -i wishlist.py.
-
3:18
Awesome, beautiful, you know what while I'm here
-
3:22
id like to add another type of product to my wish list.
-
3:25
So my other addiction is video games I've added my wish list from a couple days ago
-
3:30
to the teachers note so lets just copy and paste that.
-
3:35
Paste that up here.
-
3:38
So there we go video games.
-
3:41
Breath of a Wild, wish I was playing right now.
-
3:43
And things don't stay on this list very long.
-
3:46
Confession I've already bought all these video games.
-
3:48
I'm a little bit obsessed.
-
3:49
They're all so good.
-
3:50
Anyways, let's just display my outdated list.
-
3:52
So I think I'd like to do the same thing, I'd like to add a title and
-
3:57
then do a layout, I suppose I could just copy and paste the code, but
-
4:02
mine don't repeat yourself my dry spidey sense is going off.
-
4:06
I don't wanna duplicate that code,
-
4:08
now what if I want to change the way it's displayed,
-
4:11
I have to do that in two places so here let's solve that problem with a function.
-
4:16
So how about I make a function that expects parameters of the display name?
-
4:20
You know, like, is it a book or a video game, and also the list.
-
4:23
Let's do that.
-
4:24
But first, let's take a quick break, because I want to show you the gotchas
-
4:27
surround the passing a mutable object, like a list around.
-
4:31
Take a quick break, and come back refreshed and
-
4:33
we'll tackle this mutability issue head on.
You need to sign up for Treehouse in order to download course files.
Sign up