Flat_Eric Contemplating Data Type Conversions

Iterables

Iterables

We briefly touched on the word iterable in the previous lessons. Now we are going to look at it properly. An iterable is simply any object in Python that can be stepped through one item at a time. If Python can loop over it, it is an iterable.

You have already worked with most of these. Here is the full list of the built in iterables in Python:

What they all have in common is that Python can hand you one item at a time and keep track of where it is in the sequence. This is exactly what a for loop does behind the scenes. Each time the loop runs it asks the iterable for the next item until there are no items left.

The Vocabulary of Iteration

As you progress in Python, the way you talk about your code becomes just as important as the code itself. Understanding the correct terminology means you can read documentation, follow tutorials, and communicate with other developers without getting lost.

Here are the three terms you need to know:

So when a developer says "I am iterating over a list" they mean they are looping through it one item at a time. When they say "the iterable is a tuple" they mean the tuple is the collection being looped over. This is the language you will see in documentation, Stack Overflow answers, and code reviews so it is worth getting comfortable with it now.

In the example below we are iterating over two iterables at the same time using a nested loop. The outer iterable is a tuple of numbers and the inner iterable is a tuple of letters. For every number, Python iterates over every letter.

Iterating Over a Dictionary

Dictionaries behave slightly differently to other iterables. When you iterate over a dictionary directly, Python returns the keys only by default. To access the values or both the keys and values together, you need to use one of three built in dictionary methods.

Try the example below and then we will look at how to get more out of our dictionary.

As you can see, iterating directly over the dictionary only returns the keys. Here are the three methods you need to know and you will use them constantly throughout your coding journey:

Try each of the following in the box below and run it each time:

  • for item in user.keys():
  • for item in user.values():
  • for item in user.items():

When using .items() a very common pattern is to unpack the tuple directly in the loop using two variables. Instead of receiving a tuple on each iteration you receive the key and the value as separate variables. You will see this constantly in real world Python code.

The variable names are completely up to you. key, value is the most readable option, but shorthand like k, v is also widely used. Use whatever makes the most sense in the context of your code.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Iterating Over a Dictionary

Exercise 2: Key Value Unpacking

Exercise 3: Iterating with a Condition

Exercise 4: Iterating Over Mixed Iterables

Exercise 5: Build a Summary

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course