Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

For Loops in Python

This lesson goes into the guts of the Python for loop and shows you how iterators work. You’ll learn how to create your own iterators and get values from them using the built in next() function. You will also see the Pythonic way of iterating over dictionaries, accessing both their keys and values.

00:00 Let’s make use of Python to look at what an iterator actually is. I’m going to run Python and then enter the commands here. So, in this case, we’ll make that same list again

00:18 and assign it to a. And now we’re going to make an iterator,

00:26 using the iter() function. And if we look at that, we can see it says it’s a <list_iterator object>. So the thing we’re interested in here is the next() function, so if we go to next(itr) we get our first value out.

00:48 If we repeat that, we get the second value out. Repeat that again, we get a third value out. And if we try again now, we get an exception. We get a StopIteration exception.

01:01 This is what happens when an iterator is exhausted, when it can’t give us any more values. Now, there’s things we can’t do with it. With an iterator, we can’t go back. We can’t go backwards, there’s no previous() function, et cetera. But there you can see the way that iterators work in a little bit more detail.

01:20 Now, sometimes you will want to make a list of the elements in an iterator. So if we just generate that again

01:32 and make a list using itr,

01:40 we can see it will bring all of those up instantly and produce those. But if you’ve got a iterator which is going to produce a large number of values, that can take a very long time or use an awful lot of memory, so this is not always the best thing to do.

01:54 Often, you’ll want to use the great feature of the iterator, which is the fact that it only generates the item when it’s requested and it doesn’t have them all ready to go. So, returning to our program with our for loop in there, you now have more of an understanding of what’s happened.

02:10 When this for loop is created, the iterator is constructed here from the list, and each time around, the next() function is used to get the value from the list, and when the StopIteration exception happens, the for loop is over and then the program will move on.

02:29 Previously, you’ve seen what happens when we iterate through a dictionary. And let’s change this now. We’ll give these some values.

02:43 And we’ve seen that it actually gives us the keys from the dictionary. Let’s just change that to k to make it a bit more symbolic.

02:54 So again, if we run this…

03:04 you can see we get the keys back from the dictionary. Sometimes we might not want the keys. So, how else can we do that? Well, if we change this from just a to a.values(), we will iterate through the values instead of the keys of the dictionary.

03:25 We can see there that we get 1, 2, and 3. Other times, we might want to look at the keys and the values simultaneously, and we can do that as well. If we do that, we change this to the .items() method on the dictionary, and then we will get two values out.

03:45 So we won’t just get the key as we had one time or the values—we’re going to get both. So what we’ll do is change this to k and v. So you can see, because this is generating two items each time, we’ve got two variables to fill.

04:01 And let’s just make this a little more understandable. We’ve got a string with k being replaced there, and then v in there, in the f-string.

04:14 And we should hopefully see… there we go. We can see we’re getting the correct values for the key and the value out each time.

Avatar image for anders stenborg martin

anders stenborg martin on March 13, 2019

It crashed after Iterables and Iterators

Avatar image for emalfiza

emalfiza on March 10, 2020

You are my favorite tutor Darren, no doubt your the best.

Avatar image for hendrix55

hendrix55 on June 13, 2020

In addition to emalfiza, your english pronounciation is most clear among tutors that I can watch comfortably because I’m not native english speaker.

Become a Member to join the conversation.