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

Unlock This Lesson

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

Unlock This Lesson

Iterables and Iterators

Python for loops follow this general form:

for <var> in <iterable>:

In this lesson, you’ll learn how a for loop can be used to iterate or loop over objects such as sets, lists, strings, and dictionaries. You’ll also see that not every Python object can be iterated over, for example integers and floats don’t support iteration. In the next lesson, you’ll learn what an iterator actually is.

00:00 Let’s look at how Python actually implements a for loop. The general form is this. We’ve got for <var>—a variable name, whatever you choose—in an <iterable>. If you don’t know what that is, don’t worry, we’ll be covering it in some detail. And then we have a <statement>.

00:23 So, that’s the general form. Each time around, the <var> will be allotted the value which next comes out of this <iterable>, and then the <statement(s)> will run. Let’s look at a real example.

00:37 So let’s set a to be a list with the values ['foo', 'bar', 'baz'] in it. And then we’re going to have for i in a: print(i).

00:56 Here—our loop. Each time around, i will be allocated the next value which is available from this list. So first time it will be 'foo', second time it will be 'bar', third time it will be 'baz'. And each time around the loop, it will get printed out. Let’s have a look at that in action.

01:18 And there, just as we saw when we read through the code, we have foo, then bar, and then baz get printed out. Before we get any deeper into that, let’s look at how it happens when you try and iterate over different kinds of objects in Python. We’ve see what happens with the list.

01:34 Let’s try it with a string.

01:40 We’ll go for 'hello world', as everyone always does. And we can see this does work. When we see this, we can see it iterates over each letter in the string in turn.

01:56 Let’s try it with something else now. Let’s try it with a tuple. And as you may predict, with a tuple

02:16 it works just as we expected with a list. What about a set? There we’ve changed to curly braces and we’ve got a set.

02:33 Now, it does work—but you may notice something if you’re looking carefully. You can see that the order that we get them out in isn’t the same as the order they were in here.

02:41 So in our construction of this, 'foo' is the first entry, but it’s not come out first. And if we run the program again, we get a different result. And again—a different result. Et cetera.

03:01 So you can’t rely on the items in a set coming out in the order you put them in. Let’s try a dict. So here, we’re going to change this to a dictionary.

03:13 We’ll put some values in for each one and run that. And it seems to work just like a list did. We’ll come back to that later on, as there’s a little more to it.

03:29 Not every object in Python can be iterated over. If we try an int, this is what happens. We can see we get a TypeError because int objects are not iterable. Let’s try it with a float, 4.2.

03:55 You can see floats aren’t. What about a function? Let’s try sending it a function and see what it thinks of that.

04:08 Eh. So, 'builtin_function_or_method' is also not iterable. We’ve seen some objects which are iterable and some which aren’t. Next, we’ll look at what an iterator actually is.

Avatar image for Manu

Manu on Sept. 23, 2021

Not iterables meaning iterators?? or else can you define an exact definition for iterators? Thanks.

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 23, 2021

Hi @Manu, here’s a nice short definition:

An iterable is a container which can return its contained items one at a time. An iterator is an object which streams those contained items. So while every iterator is conceptually an iterable (and you should always make your iterators also an iterable; it isn’t much work), the reverse is not true and not all iterables are an iterator on their own.—Source

If you want the most thorough definition, you can look it up “iterator” in the glossary of the Python documentation.

Become a Member to join the conversation.