Loading video player…

Looking at Iterators and Iterables

Resources mentioned in this lesson:

00:00 In the previous lesson, I finished off the data type built-in functions. This lesson is the start of a new section, all about iterators and iterables. It’s kind of hard to define an iterable without using the word iterate, which makes it feel a bit like a self-referencing definition. Python is in the C family of languages, and in most C languages, when you want to loop over a collection, you define a counter, then visit each item in it. Python’s for loop is actually a for-in or for-each loop, where the syntax sets up this item visitation for you.

00:34 This concept is a core feature of the language, abstracting away the accessing of items in the container. This is known as iterating. There are other ways to loop in Python, but if you’re using for, you’re iterating on a collection.

00:49 Python uses duck typing. That means it doesn’t care about the definition of the object, just what methods it defines. The term comes from the phrase, “If it walks like a duck and talks like a duck, it must be a duck”.

01:03 Objects that support iteration are known as iterable. I’ll cover the details in a later lesson, but an object is iterable if it defines the correct methods.

01:12 The usual containers and sequences you use, like lists and strings, all define these methods, which makes them iterable.

01:20 Okay, time for some code. Let’s look at some of the most common built-ins you might use with iterables, especially those connected to looping. Before diving into loops, I’m going to start off with the len() function.

01:34 I’d be surprised if you haven’t come across this one yet in your Python journey, so I’ll cover it quickly for completion’s sake.

01:42 len() returns the length of a container. Here, my list has three items in it, so I get back 3. Strings are sequences, and calling len() on one returns the length of the string.

01:53 If you’re coming from other languages, this will seem a little strange. Most other languages have length operations as methods on the object, but it isn’t string.length, it’s length string.

02:05 This comes back to the idea of an iterable. len() isn’t string-specific, but a generic mechanism for finding the length of any iterable, so it’s a common function instead of being a method on the object.

02:19 Calling len() on an empty container, like an empty string, or empty list, or empty tuple, returns zero, which makes sense. They’re empty. As I mentioned, Python’s for loop is actually a for-in loop.

02:34 It takes an iterable, and in each turn of the loop visits an item inside of it. Let me start with a simple example.

02:44 The iterable in this loop is the list that I’ve hard-coded after the in keyword. The for loop visits each item in the list and puts it inside of a variable named word.

02:56 Inside of the loop block,

02:58 you can access word and do things with it, like printing it out.

03:03 Sometimes you want both the item from the iterable and a counter to go with it. To do this, you can use the built-in enumerate() function. You’ve probably seen code like this before, but if you stop to think about how it works, the enumerate() method itself returns an iterable.

03:32 So the for loop is actually iterating over that, while enumerate() itself is iterating over the list that is passed in as an argument.

03:41 Each item in the iterable enumerate() returns is a tuple containing a counter and the item being visited from the inner iterable, which in this case is the loop.

03:52 You can see this if you call enumerate() directly.

04:01 enumerate() returns a single object, an enumeration iterable. That’s what the for loop was iterating over. You’ll recall from the lesson on the list data type built-in function that you can pass any sequence to the list() function and you’ll get a list back. So if I want to see what’s inside of this enumeration call,

04:21 I can use it as an argument to the list() function.

04:26 In fact, when you’re debugging in the REPL, this is a really common thing to do. Anytime you get an iterable back from a function and you want to see what is inside of it, you can construct a list based on that iterable. You’ll notice that the counter that enumerate() starts with is zero-based. If that isn’t to your taste, the optional start argument allows you to set it arbitrarily. I have to make a note of this one. I frequently forget this feature, and it comes in handy.

05:07 Starting at one is a common use case. A hundred’s more fun, though. Since Python’s loop is for-in, if you actually want to loop over numbers, you need an iterable.

05:17 That’s what the range() function does for you.

05:24 Like enumerate(), range() returns an object. The for loop iterates over that object, and what comes out is each of the numbers in the given range.

05:35 One thing to pay attention to here is that the second argument for range() is exclusive, so doing 1, 10, like I did here, gives you 1 through 9.

05:47 Just to prove that this is an object,

05:51 there you go, even with a customized repr() string. Let’s use the list trick again.

05:59 range() has a couple of different formats. The two-argument version I’ve just used is the most common, but if you give it a single argument, you go from 0 to that number, once again exclusive.

06:12 If you’re wondering why it’s exclusive, the idea here is to use the same kind of notation as a slice. If I want the first five items of a list, I do square bracket, 0, colon, 5, and get the items with indexes of 0 through 4.

06:27 There’s also an optional argument to change the step size. By setting the third argument to 3, each number is three more than the next. Remember, the second argument isn’t how many things to return, but the top boundary, so using a stop value of 22

06:49 returns the same thing as a stop value of 20, because when you’re going 3 at a time, either number results in the same limit.

06:58 All three of the built-ins covered here have video courses and tutorials if you’d like to learn more about length, enumerate(), or range().

07:08 Next up, functions that process the items in your iterables.

Become a Member to join the conversation.