Functions: Iterables and Iterators
Iterables and Iterators. This section is going to look at functions you can use with iterables and iterators. The first one is going to be
len(), which tells you the length of the iterable that you pass it.
Here’s a list filled with some random-ish numbers. Looking at
len(a) shows us there are
9 elements within that list. We can see that that list does indeed have nine elements. Next up,
all() work on the Boolean values of the iterables that you pass to them. As we’ve seen earlier, any entry can be considered to be
False, but to simplify matters, the three lists are going to explicitly use
False. So in the case of
a, all three entries are
If we look at
any(), we can see that
True because some of them are
True. In fact, all of them are
True. If we look at
any(b), that’s also
True because it has some
True values even though they’re not all
all(b) gives us
False, because not all of them are because we have one
False value there. And as you can imagine, for
all(c) we also get
False because there are no
True values, let alone all of them being
What you may have been expecting is a reverse version of that list, and instead we get a reverse iterator object. Now, most of the time in your code, that will be okay, but some of the time you may want to see that list—for instance, when you’re debugging or printing it out. So if we do
list(e), you can see that list is
d in reverse.
03:26 The analogy I can think of is imagine having a machine which could produce cars for you. You’d have two modes it could operate in—one where it produces all the cars you need in one go, and one where it does them on demand.
03:40 That would be fine if you only needed one, but if you needed 3 million of them, you’d need to find somewhere to store them all. In this case, it’s storing all those numbers in memory, and it takes time to create them.
Generally, Python’s create-on-demand system is a more efficient way for it to work and has much lower memory demands, but sometimes dealing with this when you’re starting out means you need to cast things into a list to understand what’s happening behind the scenes. With that out of the way, let’s look at
sorted(). Here we’re defining
f as the sorted version of
Next up, let’s look at
range() is really useful. You may have already encountered it. If we enter
range() here in the REPL, we get this range from
10, which isn’t that helpful because it doesn’t really tell us what’s going on, but if we cast that into a
list, we can see we get the values
Here we can see a
range(0, 10) with a stride of
2. That gives just the even numbers,
8, and again, it doesn’t include the last number.
range() is extremely useful and you’ve probably used it already, but you may not have heard of these last two,
enumerate(), which are easier to understand in the context of a program, so we’re going to switch over to code now.
If we see that in action, it works exactly as we’d expect, printing each name on its own line. But quite often it’s important to know which number on the list is which index in the list. The way you would probably do this if you weren’t aware of
enumerate() is to create a
count variable which is initialized before the loop starts.
But Python provides us with
enumerate(), which makes a much cleaner way to do this same functionality. We’re going to delete the
count variable, we’re going to delete the increment of
count, and then we’re going to wrap
players in the
enumerate() function, which outputs two things. It outputs the normal
player, but it will also output the
count—a zero-indexed counter of the number of times a loop has run. So we have much neater code, you don’t have to worry about initializing or incrementing the
count variable—which everyone has forgotten at some point—and running the code gives us exactly the same output as before, but it’s a much neater piece of code to read.
You’ll want to create tuples with these paired together. The way you would initially do this would probably involve a
for loop, so in this case, using an index variable combining
range() and the
len() of the
If you’re not aware of the
zip() function, this is probably the kind of thing you would do many times. For there’s a much simpler way to do this, and as we can see, we can use
merged_2, it uses the
zip() function with those two lists
continents, and that’s that entire
for loop replaced.
When we run this, they both produce identical output, but this is a much more complicated equivalent to this. The
zip() part is much simpler than these three lines where you create an empty list and a
for loop. It could have been done with a list comprehension but still would have been complicated. But
zip() works faster, is much easier, and is much less error-prone than the manual approach.
It’s important to remember that this is a zip in terms of a zipper on your clothes, not in terms of a compressed file creator.
zip() is incredibly useful whenever you have lists you need to merge together.
Iterables and Iterators:
next(). The last thing we’re going to look at in this section is the creation of an iterator such as we saw earlier on. It’s possible to create an iterator using any iterable. We’re going to use a list in this example.
It has a method
next(), which pops the next value off of that iterator until it’s exhausted, when you’ll get a
StopIteration exception. Let’s make an example now, so here
a = iter() and we’re just going to pass it a list directly of
[1, 2, 3, 4, 5, 6], to keep it nice and simple.
And once it’s exhausted, if we try
next() again, we get a
StopIteration exception. This is the way that any iterable works behind the scenes with Python, so anything which can take an iterator will work exactly the same way with an iterator you’ve created using the
Become a Member to join the conversation.