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.
iterationdefinition in Python Terminology.
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.
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.
for
loops don't have (or need) indexesPython'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.
for
loops simpleWhen 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.
We don't learn by reading or watching. We learn by doing. That means writing Python code.
Practice this topic by working on these related Python exercises.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces.
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.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!