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