Python's "for" loop

Share
Copied to clipboard.
Series: Looping
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
02:50

You can use a for loop to loop over any iterable (iter-able). Anything you're able to iterate over can be looped over with a for loop.

Looping over an iterable

Lists in Python are iterables. Here we have a variable favorite_fruits that points to list of strings:

>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"]

We can loop over (aka "iterate over") this favorite_fruits list using a for loop:

>>> for fruit in favorite_fruits:
...     print(fruit)
...
jujube
pear
watermelon
apple
blueberry

While executing that loop, Python takes each of the things inside of favorite_fruits list and gives us access to each of those items in order, using the variable name fruit. We're using that variable name to print each object (all strings in this case) out.

You can use this for anything that's iterable. An iterable is anything that can be looped over using a for loop.

Python's for loop is called a for each loop in many other programming languages. In fact, the foreach Wikipedia page notes that Python's for loop is the equivalent of foreach in many languages.

Looping using indexes, the wrong way (don't do this)

Sometimes people write their for loops like this:

>>> for i in range(len(favorite_fruits)):
...     print(favorite_fruits[i])
...
jujube
pear
watermelon
apple
blueberry

There's a couple of reasons I don't recommend this. The first one is that PEP 8, the official Python style guide, recommends against this. Another reason is this only works with sequences.

A sequence is an iterable that you can index (using square brackets) starting from 0 until 1 less than the length of the sequence. Strings, tuples, and lists are all examples of sequences. Sets, dictionaries, files, generators, and lots of other iterables are not sequences.

The biggest reason not to use this is that we're doing a lot of unnecessary work here. We're using range(len(favorite_fruits)) numbers, representing indexes, in the variable i, and then using those indexes to then look each of the items up in our list. This extra work is like entering the backdoor of a building instead of the front door: you're still entering a door in either case but you have to walk much farther to go through the back door.

Not only that, but we're also focusing on the wrong thing: we're focusing on the index. We're giving a variable name, i, to the index. We're not giving a variable name to the most important thing here: the actual item we're working with.

Python's for loops don't have (or need) indexes

Python's for loop gives a name to each of the actual items we're working with (fruit in our case).

>>> for fruit in favorite_fruits:
...     print(fruit)
...
jujube
pear
watermelon
apple
blueberry

If something is important, it deserves a name. Python's for loops names the items themselves, not indexes.

Keep your for loops simple

When you're writing a for loop in Python, keep it simple, keep it as simple as possible. If you do need to count upwards as you loop, the built-in enumerate helper function can help you with that, and we can talk more about that later.

Series: Looping

Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Our for loops in Python don't have indexes.

This small distinction makes for some big differences in the way we loop in Python.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video
02:50