Heads up! To view this whole video, sign in with your Treehouse account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Level Up Your Python Code with Enumerate, Zip, and Map with Megan Amendola
24:17 with TreehouseTake your Python code to the next level with three built-in methods: enumerate, map, and zip. These three functions can help you keep your code simple and DRY.
This video doesn't have any notes.
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
Today, I have to introduce
our first speaker.
0:08
She is a teammate and a colleague and
just a one wonderful person.
0:11
She's out here in the Midwest for
me, y'all.
0:15
Megan Amendola has been with
Treehouse over three years.
0:17
She has a bachelor's degree in
Speech Pathology and Audiology, and
0:22
a master's degree in Education.
0:27
After teaching for five years,
she decided to make a switch, and
0:29
to Treehouse's full stack
JavaScript tech degree.
0:34
She continued her education,
learning UX design and Python, and
0:37
shares the knowledge with
our Treehouse students.
0:41
Please welcome Megan Hello everyone,
0:45
I hope you're having a wonderful day.
0:50
Thank you, Toni, for that introduction.
0:53
And we're gonna be talking today
a little bit about some Python.
0:56
If you don't have any Python knowledge,
that's totally okay.
1:00
And let's get into it.
1:07
So I'm going to share my screen, share.
1:08
Sorry for that little pop into the void.
1:15
My little share screen.
1:19
Yep, I'm one.
1:24
I'm one.
1:26
Oops, there we go, my bad,
it's the morning [LAUGH].
1:27
Okay, so we're gonna be talking about
levelling up your Python code with
1:32
enumerate, map, and zip.
1:36
These are three built-in Python functions.
1:38
Like Tony said, I started out like a lot
of our students here at Treehouse.
1:45
I don't have a background in coding.
1:49
My background is in speech pathology,
audiology, and education.
1:51
And I decided to make a switch.
1:56
I took the full stack tech degree and
1:58
fell in love with coding, and here I am.
2:02
The reason why I picked this talk
today is, I think what a lot of people
2:07
struggle with is kind of where do I
go from beginner to not a beginner,
2:12
whatever that title is,
whether it is intermediate, coder, or
2:17
next level, whatever that title is.
2:21
I think a lot of people have a hard time
figuring out what that switch is kind of,
2:24
where you go from super beginner
to someone who knows to code.
2:30
And in my opinion, it functions like
the ones we're gonna talk about today.
2:34
Those are the ones that kind of take you
over the edge and no longer a beginner.
2:38
These are the kinds of things that
can make your code more simple.
2:44
It can dry up your code.
2:49
And it can just make the things that you
do in the way that you problem-solve
2:52
a little bit more advanced,
a little bit more dynamic.
2:56
So that's why we're gonna
talk about those today.
2:59
I am going to be jumping into code.
3:03
So if you want to code along with
me this is your forewarning.
3:06
You can open up a treehouse workspaces
if you're a treehouse student.
3:10
Python is already installed in there and
just create like an app.pi file.
3:15
If you wanna work locally,
open up your favorite text editor.
3:20
And you would need to have Python
installed on your computer to use
3:24
the Python we're about to do.
3:29
But you can open up a file in your
favorite text editor to follow along.
3:31
All the codes are already up on GitHub
if you just wanna grab it as notes.
3:37
The GitHub is,
if you search under treehouse festival,
3:42
Level up your Python Code, you'll find it.
3:45
So let's get started.
3:48
The first one we're gonna
talk about is enumerate.
3:52
So enumerate, what does it do?
3:57
Very simply,
it adds a counter to an iterable.
4:00
So sometimes, you might create
like a count variable, and
4:06
then when going through a for-loop,
4:10
you might do like count plus equals one to
count through each thing in your iterable.
4:12
And enumerate does that for us.
4:18
You don't have to create that variable.
4:20
And you can also set
where the counter starts.
4:21
This one, simply,
4:24
you would use it when you need to
count something while you iterate.
4:25
So it's kind of as simple as that,
that's as easy as it is.
4:28
So we're gonna jump into some code.
4:33
I'm gonna switch over to my text editor.
4:34
I'm gonna move this backstage over here.
4:39
That way, I can see just in case
something's going wrong [LAUGH].
4:41
Cool, looks like everything's going right.
4:45
Nope, don't want that.
4:48
Full screen, please.
4:49
Thank you.
4:50
Okay, so I have a list here.
4:51
You can really make a quick list,
if you're following along, of anything.
4:56
It really doesn't matter for
what we're doing right now.
4:59
Seems we have a list of flowers.
5:03
And it's four flowers, pretty simple.
5:05
For a quick review, just to make sure
we're all on the same live page,
5:08
a for-loop looks like this.
5:12
So if we wanted to loop through our list,
5:13
we could do, for flower in flowers.
5:18
.And then let's just, print(flower).
5:22
Save, and let's run it in the console.
5:26
This is enumerate.py.
5:29
And you can see, that's my little screen
share thing, can we move that over there?
5:32
There we go.
5:37
I get sunflower, daisy, rose, peony.
5:38
So it grabs each item in our iterable,
print it to the screen.
5:40
But with counting, if we wanted to
count each flower, let's say we,
5:44
this is a database of flowers and
we're listing out in the console,
5:50
a numbered list of all the flowers that
we sell at our nursery, just for an idea.
5:55
I also use counts a lot of the times
with like games, counting turns,
6:02
and things like that.
6:06
So you might have done something
previously like this, so you count = 0.
6:08
And then you do, for flower in flowers.
6:13
Oops, I hope I spell it correctly.
6:19
There we go.
6:22
And then let's do,
6:24
print( count, flower).
6:27
And then we would do, count += 1.
6:32
So our count is going to start at zero and
it's gonna go to one.
6:36
And just so
our console doesn't get full of stuff,
6:39
I'm going to comment out
the previous for-loop.
6:43
That way our console just doesn't get so
busy.
6:47
Okay, so you see we get zero sunflower,
one daisy, two rose, three peony.
6:51
So it gives us a count or a number that
goes with the items in our iterable.
6:55
Now, let's see what enumerate
does if we use that instead.
7:01
So we have for, and now enumerate is
going to give us two things, right?
7:07
It's going to give us a single item
from our iterable and the count number.
7:12
So we need to unpack
that as two things here.
7:18
So I'm gonna unpack it as count, and
7:22
then as the flower, in,
and gonna do enumerate.
7:27
And inside, we're going to pass in
our iterable, which is flowers.
7:33
I'm gonna keep typing this out, and
then I'm gonna say what this is one more
7:39
time just to make sure
everyone's following.
7:43
So, print(count, flower).
7:45
Make sure you save them.
7:50
Okay, so enumerate gives us,
oops, there we go,
7:51
enumerate gives us two things.
7:56
We pass in an iterable, and
7:59
then it pairs each item in the iterable
with a number or a counter.
8:01
And then you unpack that as
two variables in your for-loop
8:06
in order to access those two items.
8:11
So, I'm gonna clear my console,
now run it again.
8:13
Oops, let me do this, just so
it's very clear what we're running.
8:19
I am gonna comments out
what we had before.
8:22
Here we go.
8:29
Okay, so you see we get the exact
same result, zero sunflower,
8:30
one daisy, two rose, three peony.
8:34
So we took one, two, three, four lines
of code down to two lines of code,
8:36
which may not seem like a ton
in the grand scheme of things.
8:43
But it is a way for
you to look through your code and
8:48
see how you can shorten things, how you
can make things a little bit more simple.
8:52
Enumerate is a little
bit more complicated,
8:59
a little bit more advanced than doing
count, count += 1, in my opinion.
9:02
So I think any potential employer looking
at your code would be like, look at that,
9:08
they're using enumerate,
instead of doing a count variable.
9:12
They obviously know Python a little
bit better than maybe somebody else's
9:14
work that they looked at.
9:19
So that is essentially
how enumerate works.
9:21
So it's kind of simple.
9:24
That's what it does,
there's not a lot to it.
9:25
But one thing that's very
important to know about enumerate,
9:28
we come down here, I'm gonna do
enumerate passing the iterable.
9:32
And I'm gonna try to do
an access like this,
9:37
like I'm trying to access the first item,
which you think maybe it would
9:40
give you like a tuple that has the count
and the flower paired together.
9:45
But let's see what happens.
9:50
I'm gonna save.
9:51
I'm also going to comment that out too,
9:52
just to make our console
a little bit clear.
9:55
Run it again, and
you can see we get an error.
9:59
Enumerate object is not subscriptable.
10:03
You can't access and
enumerate using this method.
10:06
You have to access that using a for
loop or you have to translate it.
10:11
So, if I were to print this.
10:16
Whoops, from the beginning, there it goes.
10:18
Print, save and
10:21
you do a clear.
10:25
Let's try this again.
10:29
You can see if I print our enumerate
call with flowers passed in,
10:30
I get an enumerate object.
10:35
And this is going to be the same for each
of the things we're talking about today.
10:38
The enumerate object is its own object.
10:41
So, you can't just access
it like you would a list,
10:44
because it's no longer a list.
10:47
So, you're probably thinking,
well, how do I access it?
10:50
Let's turn it, oops, back into a list.
10:53
So just to recap, we have enumerate
where I'm passing in flowers,
11:00
then I'm translating that into a list and
then I'm printing that result.
11:05
And you can see, now we have those
tuples where we have the count,
11:13
plus the flower count plus
flower all the way through.
11:17
And if we wanted to Do just the first
11:20
one, We could do that and
we get that first tuple.
11:25
So, that's another way
to access your items.
11:30
And for the last thing on enumerate,
I said that you can pick where you start.
11:33
So, that's important cuz sometimes you may
wanna start at zero, sometimes you may
11:39
wanna start at one, sometimes you
may wanna start at a million.
11:44
Account, flower and enumerate.
11:47
Gonna pass in your iterable, and
11:53
then you're gonna set start equal
to whatever number you want.
11:55
Let's do ten just so
it's super clear that it's different.
11:59
And print, Count, flower, save.
12:03
And to comment that out too.
12:13
And clear.
12:16
And there we go.
12:20
Sunflower, daisy, rose, starting at 10.
12:21
So, pretty simple, but can be pretty
powerful too Let me hop back over.
12:24
Okay, so that was enumerate,
12:36
the second one we're going
to talk about today is map.
12:37
And I don't know why every time I say map
I think of the Dora the Explorer song,
12:40
it always gets stuck in my head.
12:44
So, what does map do?
12:47
Map is going to apply a function
to each element in an iterable.
12:49
You can either create a function, which
is really good if you're doing something
12:55
longer for each element,
it makes sense to kind of break out.
13:00
Or you can create a Lambda function,
13:04
which is great if you're doing something
super short, and I'll show both.
13:06
So, when would you use it?
13:10
When you wanna do the same thing to every
item in an iterable without a loop.
13:12
Because without the loop, it can sometimes
be more efficient and it can save memory.
13:18
Cuz it's going to do one thing at
a time versus saving everything, and
13:25
then doing something to each item.
13:29
So, we can be a little more efficient.
13:32
So for example, well,
let's jump into the examples.
13:35
Let's jump into code.
13:38
Escape, hop back over here.
13:40
Clear, and let's pop over to map.
13:46
Okay, nope, that's it.
13:49
Here we go, map.
13:52
So, we have our same list,
same flowers list.
13:53
I'm gonna pull up my notes over
here just so I'm on the same page.
13:56
Let me look at our time.
13:59
I'm gonna do a little copying and pasting.
14:02
Sorry, if you're following along,
just for time.
14:04
I wanna make sure I get
through everything.
14:08
So, I'm gonna copy this over.
14:10
I'll talk about what I
just put on the screen.
14:15
Okay, so here we have a regular loop, I
have created an empty list called plural.
14:18
And I'm looping through our flowers,
14:25
and if the last letter is a y,
I'm removing that y and
14:28
adding ies to pluralize,
like daisy into daisies.
14:33
And then, otherwise if it doesn't
have that, we're adding s, so,
14:38
sunflower to sunflowers.
14:42
And then we're going to
print this list at the end.
14:43
So, that is saved.
14:47
So, let's do python
14:49
You can see we get sunflowers,
daisies, roses and peonies.
14:56
And this is probably something that
a lot of you are familiar with.
15:00
I feel like I do stuff like this a lot
when I'm working on coding, challenges,
15:03
projects, things like that.
15:08
I have a for loop and then I do
a bunch of stuff inside that for loop.
15:10
So, let's take a look at what
this looks like with map instead.
15:14
Let me move this too,
15:21
that way if you're trying to type this
I can keep this on the screen here.
15:23
Okay, so essentially,
I'm going to take what's inside of our for
15:29
loop and I'm gonna pull that
out into its own function.
15:34
So, I'm gonna just copy this
over again just in case of time.
15:39
So, map,
we have pluralize passing in the word.
15:45
If the last letter is y,
add ies, if it's not, add s.
15:48
So, exact same thing,
just pulled out into a function.
15:52
Now, plural, here, I've created a variable
called plural and I've called map.
16:03
And you pass in the function and
then you pass in the Iterable.
16:10
So, this function right here, just like
we did up above with this for loop,
16:15
is going to apply to each element in
the iterable that we have passed in,
16:20
which is our flowers.
16:25
And now because it's a variable, you're
not gonna see anything on the screen.
16:28
So, I'm gonna do print plural, save, and
16:33
I'm gonna comment out our for loop here.
16:37
Okay, and let's run this?
16:45
And you can see, like I said before,
we get a map object this time,
16:54
instead of an enumerate object.
16:58
Because each of these is going
to turn into their own object.
17:00
So in order to view what
we have going on here,
17:03
I need to do list and
translate our map object into a list.
17:08
So, let's now run this again.
17:14
And now you can see, we get the same
list that we did from our for loop.
17:16
We have sunflowers,
daisies, roses and peonies.
17:19
And if you take a look at the code,
we have what, one, two, three, four, five,
17:23
six, technically, seven lines of code.
17:28
And before we had one, two, three,
four, five, six, seven lines of code.
17:31
So, it's about the same for
amount of code, but again,
17:34
I think it shows your
knowledge of Python better.
17:40
And there will be times and there may
be times where things like memory and
17:44
time are very important in your project
that you might wanna use map instead of
17:48
a for loop because it can be faster.
17:53
Now, another thing to know about this
is that you can use built in functions.
17:57
Copy this over because I think
I'm running out of time.
18:04
I talk really fast to
myself at home apparently.
18:07
Okay, as you can use
other built in functions.
18:12
So, you can use like length here,
18:18
the length function which usually
you see it with the two parentheses.
18:21
But when using map,
you don't wanna use those parentheses.
18:27
So, here this is going to get the length
of every string inside of flowers and
18:31
it's gonna turn it into a list and
we're gonna see that list.
18:35
So, let's check that out.
18:39
Oops, I forgot to save,
I hope you probably saved first.
18:42
Here we go, so we get 9, 5, 4, 5.
18:46
So, any already built in functions like
length, you can also use with map.
18:48
You just don't wanna use the parentheses.
18:54
And the last thing I wanted to
show was a Lambda function.
18:57
And again, if you're following and
you're like, my God,
19:04
she's copy pasting now and
I can't keep up.
19:06
All of this is already
saved on a GitHub repo, so
19:07
you will have all the code that we have
that I'm doing right now available.
19:10
So, here we're doing a Lambda function.
19:16
I created a list of numbers and
then I'm doubling them.
19:19
So, Lambda function,
you use the Lambda keyword,
19:24
and then just like a function,
this is the parameter I'm passing in.
19:28
The parentheses here is saying, now I'm
starting what's inside of my function,
19:33
which is I'm going to take that
parameter that I passed in this number.
19:38
I'm gonna multiply it by two, and
I'm gonna do all of that on this iterable.
19:42
So, this is our function,
which is just taking the number,
19:50
each number and
multiplying it by two for this iterable.
19:54
And I'm gonna turn that into a list and
then I'm printing it.
19:58
So, let's take a look.
20:00
And I didn't do the one before, so
this is the one we're looking at.
20:07
So, 1 times 2 is 2, 2 times 2 is 4, etc.
20:11
So, map can be super helpful, can allow
you to do things a little bit quicker and
20:15
save a little bit of memory.
20:20
Close this, and we pop back over to map.
20:23
And the last one we're going to do is zip.
20:30
So zip returns an iterator of tuples.
20:33
So it's going to aggregate
iterables into tuples.
20:36
And very, very important, it works until
the shortest iterable is exhausted.
20:40
So, if you have two different sizes of
lists, say one has seven things and
20:45
one has five, it's only going to work
until it exhausts the short one.
20:50
And then why would you use it?
20:55
Whenever you wanna iterate over more
than one iterable at the same time,
20:58
that's what zip's gonna help with.
21:01
So let's take a look.
21:03
So here you can see I
created a second list.
21:07
These are of trees, and
I have one, two, three, four.
21:10
I have four and four,
so same number in each.
21:15
And same, I'm gonna do a little copy and
pasting to save time.
21:18
My apologies for
anyone who's following along.
21:20
Okay, so zip, I just jump in right to it.
21:27
You pass in all of the iterables
that you wanna zip together.
21:30
It's kinda like zipping
a file on your computer.
21:34
And I'm saving that to
a variable called plants.
21:37
And I'm gonna print both plants and
translate it into a list so
21:39
we can see it both ways.
21:42
I can see a zip object, but
if I translate it into a list,
21:48
I can see the tuples where it's paired all
of the elements of our two lists together.
21:52
If I wanted to loop through
the items that I have now,
22:00
I can do for plant in zip, and
I get flowers, pass in flowers and trees.
22:10
And print plant,
I'm going to get each individual tuple.
22:15
But instead I can do tree, flower and
22:20
break it back out into each of those tree,
flower, save.
22:25
Here, and you can see instead I get
the pairings of the trees and flowers.
22:33
And I did a mistake and I'm glad I did it.
22:40
Whichever order you pass in here
needs to match the order here.
22:42
Cuz you can see I have tree, flower,
but I really have flower, tree.
22:47
So that can get a little mixed up and
a little confusing.
22:52
So this actually needs to be flower, tree.
22:57
And then, of course,
if I were to delete, let's say, pear,
23:14
Save, and if I were to run this again,
you can see I only get three
23:21
pairings out now instead of four
because it ignores this last one.
23:26
Because it only goes until
one iterable is exhausted.
23:31
So it's very important to know.
23:36
And I believe that it's about time.
23:40
Sorry, I kinda ran through
that last one really fast.
23:41
The GitHub link is open.
23:44
Let me pop this back up.
23:47
So, that is the link to the GitHub repo,
you can see it right there at the bottom.
23:51
If you search Treehouse Festival,
level up your code, you will find it.
23:54
And it's under my GitHub,
which is MissMeg.
23:59
If you find it under there,
but thank you all so much.
24:02
I'm sorry I went so fast at the end.
24:04
[LAUGH] But check out the repo,
it has all the notes from today.
24:06
And keep coding, keep looking up your
Python knowledge, keep leveling it up and
24:09
reach out if you have any questions.
24:14
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