Table of Contents
- What is Control Flow in Programming?
- How Do You Use Loops in Python Code?
- How For-Loops Are Created Using Iterable Objects
- What Are For Loops in Python Code?
- How to Write For Loops in Python
- How to Use For Loops With Variables For Lists
- How to Use For Loops With Equations
- How to Use For Loops with Strings
- How to Use For Loops With Running Totals
- How to Use For Loops in Nested Loops
- How to Use Nested For Loops with Nested Lists
- How to Use For Loops with Tuples
- How to Use For Loops with Dictionaries
- How to Use For Loops and the Range Function
Welcome back to the latest article in my Intro to Programming series. In this article, I'm going to start talking about another control flow structure. Namely, the for loop.
What is Control Flow in Programming?
In previous articles, I started talking about control flow. In general, control flow refers to the order in which lines in a computer program get executed, as well as how many times each line of code gets executed. It might be a bit of a strange term if you haven't come across it before, but perhaps it will help if you think about it this way:
- Control refers to computer code, because computer code controls what the computer does.
- Flow is how this computer code is executed.
Similar to a river that flows and may have different incoming streams or ramifications, if statements allow you to specify branches in our computer code.
To continue with the river analogy, rivers may also occasionally have whirlpools, areas where the water simply swirls in circles until it escapes the whirlpool and continues its normal flow. I made a small diagram to reiterate this point:
Image Source: Edlitera
This is called a “logical flow diagram." You can think of each box here as a line of code that makes up some program. It doesn't matter what the program does, I'm only looking at the order in which the lines get executed.
The code normally flows in a straight line from top to bottom, following the green arrows, starting with the first line at the top - the blue box. Code line 1 is followed by code line 2. But at some point, I encounter an if statement, some condition that forks the execution.
What is a Loop in Code?
If the condition is True, in this case, I continue with lines 4, 5, and 6, and then the program finishes. But if the condition is False, I execute code line 3, following the red arrow. Then, after I run that line, I again ask if the condition is True. Maybe this time it is True and I finish the program. Maybe it's not, and I have to go back to code line 3 via the red arrows.
The code path marked by the red arrow in this diagram represents a loop. It looks a bit like a loop, too. As you can see, the loop is a control flow structure that allows me to execute a line of code more than one time.
In this case, code line 3 will get executed as long as the condition I'm checking for is False. So how many times can we execute the same line of code? Well, a million times if you need to. Or more. It doesn't matter. The loop will usually get executed as long as some condition is True or False.
What is an Infinite Loop in Code?
The loop can be on either branch of the condition. In my example, it's on the False branch, but I could also put it on the True branch.
You might ask what if, in this diagram, the condition is never True? Well, in that case, I am stuck in the loop. I have what is called an infinite loop. In that case, my program will simply never end.
Sometimes, you might deliberately want to write a program that never ends because each time it goes through the loop it actually does something useful and you want it to keep doing that until the end of time. But most often, an infinite loop is an indication of a problem with your logic.
Every programmer will, at some point, accidentally writes an infinite loop, so it's a bit of an inside joke among programmers. It's perhaps why Apple named its address at the new headquarters 1 Infinite Loop, Cupertino, California.
How Do You Use Loops in Python Code?
I want you to have a visual image of what loops are about. Whereas if / elif / else statements are a bit more natural, because you'll always use them to express preferences or conditions, loops are not something that you use or encounter very often in day-to-day life. Except maybe for those occasions when you play a song you really like over and over on repeat. That's a loop, too.
So, that's what a loop looks like, in general. The concept is universal to computer programs, but the devil is always in the details, as they say, so next I'm going to focus on ways in which Python specifically implements loops.
There are actually several ways, but for now I'm only going to focus on one: the for loop.
What are Iterable Objects in Python Code?
Before I get into the syntax details and code examples, there is one more concept I have to introduce, and that is the concept of an iterable object:
Image Source: Edlitera
Let's start with a simple example:
Image Source: Edlitera
If you have a box full of chocolate truffles, you can enumerate the truffles. In other words, you can iterate through the contents of that box.
One example of iteration, or enumeration, is the process of taking out one truffle at a time and counting it.
Another example is a group of people:
Image Source: Edlitera
The group is made of individual people and I can enumerate them, or, to put it differently, I can iterate through the group.
How For-Loops Are Created Using Iterable Objects
So now, perhaps you're thinking, what does that have to do with loops? It's best I show you through a bigger example:
Article continues below
Want to learn more? Check out some of our courses:
Using Items as Iterable Objects
Let's play a game.
Let's say I have my box of chocolate truffles again and for each truffle in the box, I'll take the truffle out and I'll think of a number between 1-10, and I ask my best friend to guess the number. If they guessed it right, they get the truffle. If not, I eat the truffle.
Chances are I'll probably eat most of the truffles, but that's ok.
Image Source: Edlitera
Did you notice what I did here? I created a loop.
The steps of taking one truffle out of the box, thinking of a number, asking my friend to guess - these are all steps that get repeated over and over until I run out of truffles.
The condition that ends the loop is that I ran out of truffles, so unless you have access to infinite supplies of chocolate truffles, which would make me very jealous, I will eventually run out and the game will end. This is, of course, not an infinite loop.
Another thing to notice is that this truffle loop is possible because I can iterate through the box of chocolate truffles. In other words, it's possible because I can enumerate the individual items that make up a larger object and perform some actions for each item.
Using Letters as Iterable Objects
It's very important that you understand this intuitively before you move to writing code examples that are more abstract, so let's go over another example.
Let's play another game.
Let's take the current day of the week - let's say Tuesday:
Image Source: Edlitera
Not my favorite, but it will do for now.
Image Source: Edlitera
How do you go about this?
I'll start with the first letter and I'll try to think of an animal. Then, I'll move on to the next letter, etc.
As I'm doing this, I'll need to keep track of the current letter so I can write this down:
Image Source: Edlitera
Just for extra practice, I'm going to do this in Python syntax, so I'm going to say: current_letter = 't'. Notice the quotes here, because 't' is a string.
What's an animal that starts with t? Let's say Tasmanian devil.
Image Source: Edlitera
So I can print(Tasmanian devil).
Now we move to the next letter.
Image Source: Edlitera
That's u, so I can set current_letter = 'u'.
The only one I can think of is urchin, so I print('urchin').
Image Source: Edlitera
And so on, and so forth. I won't go over the remaining letters, but you get the point.
Using Objects as Iterable Objects in Python
Image Source: Edlitera
To get back to my main point, iterable objects are objects that are made of parts or items that can be enumerated.
In this case, you say that the object can be iterated through. Also, as you've just seen, iterable objects allow you to create loops, where conceptually, for each item that is part of the iterable object, you can perform some actions.
So what are some iterable objects you've learned about in Python?
Image Source: Edlitera
Well, for example, all the objects that are sequences are iterable:
- Strings are sequences of characters, so for each character in a string, you can perform some actions. > Into to Programming: What Are Strings in Python? >
- Lists are ordered sequences of items, and for each item in a list, you can perform some actions > Intro to Programming: What Are Lists in Python? >
- Tuples are also ordered sequences of items and again, for each item in a tuple, you can perform some actions. > Intro to Programming: What Are Tuples in Python? >
- Dictionaries are sequences of pairs consisting of a key and a matching value, and for each pair in a dictionary, we can again perform some actions. > Into to Programming: What Are Dictionaries in Python? >
Now that you've built some intuition around for loops, you're ready for some coding. Let’s go over the anatomy of a for loop in Python and explore some of its most common use cases.
What Are For Loops in Python Code?
Let's explore the anatomy of a for loop in Python:
Image Source: Edlitera
A for loop is really just a type of loop in Python that's created by iterating through the items that make up an iterable object.
Image Source: Edlitera
In this example, I have a for loop that iterates through a list which contains the integers 1, 2, 3 and 4, and for each integer, it performs the action of printing that number.
Let's take this apart and look at the components that make up the loop:
Image Source: Edlitera
First, notice the for and the in keywords highlighted in green. These are required to build the loop, and you naturally encountered them in your earlier examples:
Image Source: Edlitera
One thing that often confuses beginners is that current_item variable there. That's a variable that refers to the current item, but you can name it whatever you want.
Image Source: Edlitera
For example, if you're talking about truffles in a chocolate box, you can call it current_truffle. As with all variable names, you should make it as descriptive as possible.
This variable is automatically set by Python. So, Python performs the act of going through each item in the iterable object, which, in this case, is the list 1, 2, 3, 4, and then, at each step, it stores the current item in that variable.
This is important because in doing so, it makes that current item available to you:
Image Source: Edlitera
Next, I have the iterable object. I already covered this in detail, so I won't go over it again, but this is just to tell you where in the for loop structure you need to place it:
Image Source: Edlitera
And finally, on this line, I end with a colon, similar to how I did with if / elif / else statements. The colon is very important, so don't forget it:
Image Source: Edlitera
Below, I have the set of lines of code that I want to execute for each item in the collection or sequence that I am iterating through. In this case, I'm only doing a single thing. I'm printing the current_item. So, if I run this code, it will first print 1, then it will print 2, then 3 and finally 4.
Notice the four spaces indentation. It can also be a tab, as I mentioned earlier. This, again, tells the Python interpreter which of the code is inside the loop vs outside the loop.
That's all for the anatomy of a Python for loop. Pretty simple, right?
Next, let’s dive into writing some code using for loops.
How to Write For Loops in Python
Let's start by actually writing the code example I went over earlier: for current_item in [1,2,3,4]: print(current_item). If I run this code, you'll see that the numbers 1, 2, 3, and 4 get printed in order.
Let's change this code to print something before the actual number. Let's say for current_item in [1,2,3,4]: print('current item is') print(current_item). You see that each of the two print lines gets executed for each of the integer numbers in the list as shown here in Jupyter notebook:
# Let's create our for loop
for current_item in [1,2,3,4]:
print(current_item)
# Our output will be 1, 2, 3, 4
# If we change our code just a bit:
for current_item in [1,2,3,4]:
print('current item is')
print(current_item)
# Our output changes to include two lines:
#current item is, 1, current item is, 2, current item is, 3,
#current item is, 4
Just to drive home the fact that you can pick whatever name you want for the variable name here, let's change this to for boo in [1,2,3,4]: print(boo), and you see that, again, it prints all the numbers.
So at each step of the loop, that variable gets updated to store the current item in our sequence. It starts by being 1, then it becomes 2, etc.
# Let's pick another name for our variable here
for boo in [1,2,3,4]
print(boo)
# Our output is 1, 2, 3, 4
How to Use For Loops With Variables For Lists
Another thing I want to mention is that instead of the actual list, you can use a variable that stores a list.
So let's say that you have a variable called truffles, and it stores the list [1,2,3,4,5]. Pretend that each truffle in the box has a number on it, so that's how you'd represent it in computer code. Say that you want to go over each truffle in the box, and just print out its number. You can just write for some_truffle in truffles: print(some_truffle).
I named my current item some_truffle, but again, you can name it whatever is appropriate in each context.
Note that, instead of the explicit list, you now use the name of the variable that stores the list, which in our case, is this truffles variable.
# Let's create our list:
truffles = [1,2,3,4,5]
# and our for loop
for some_truffle in truffles:
print(some_truffles)
# Our output will be 1, 2, 3, 4, 5
Also note that I don't actually have to use that current item in my logic.
For example, I can just write truffles = [1,2,3,4,5] for some_truffle in truffles: print('Guess what number I have in mind!'). And you see that the result is that you print "Guess what number I have in mind!" 5 times.
Why 5 times? Well, because this print statement is executed each time you pick a truffle from the box, and I have 5 truffles to pick:
# Let's use our list
truffles = [1,2,3,4,5]
# to create our for loop
for some_truffle in truffles:
print('Guess what number I have in mind!')
# Our output will be Guess what number I have in mind!, Guess what number I have in mind!,
#Guess what number I have in mind!, Guess what number I have in mind!,
#Guess what number I have in mind!
Now, you may say that you could achieve the same result by printing "Guess what number I have in mind!" 5 times by just writing 5 print statements, and that's correct.
But there are two problems with that approach. The first is that, instead of 5, you might have 100 chocolate truffles, or perhaps even 1000. It is bad form, not to mention pretty tedious, to write a program that has 1000 repeated print lines.
The second issue is that, if you read this list from a file, or as input provided by a user of your program, there's no way to know ahead of time how many items that list will have.
So that's why for loops are essential. They allow you to traverse a sequence of items and execute some code for each item, regardless of how small or large that sequence is. As a programmer, you don't have to think about it. You only need to worry about the part that gets repeated in each loop.
How to Use For Loops With Equations
Let's solve the following problem.
Someone gives you a list of numbers and asks you to print the result of multiplying each item in that list. If they give you the list consisting of numbers 1, 2, 3, they want you to print 2, 4, and 6. How would you do that?
Well, you don't yet know how to read input from users - I'll go over that in an upcoming article - but let's assume that somehow you read that list and let's say it consists of the numbers 1, 2, 3, 4, 5. So you can define a variable called input_nums short for input numbers, and let's set it to be the list [1,2,3,4,5].
Next, you write a for loop that goes over each element in that list, and outputs the result of multiplying it by 2. You do that by writing for num in input_nums: print(num*2). So here, again, this num variable will in turn take the values 1, then 2, then 3, then 4, and finally 5.
In each of the steps of the loop, you'll take that value, multiply it by 2, and print the result. If you run this code, we see that the output is 2, 4, 6, 8, 10, which is indeed what you wanted:
# Let's create our list
input_nums = [1,2,3,4,5]
# and create our for loop
for num in input_nums:
print(num * 2)
# Our output will be 2, 4, 6, 8, 10
Here's an even more interesting problem. Given a list of numbers, print only the ones that are multiples of 5. For example, 5 is a multiple of 5, so is 10, 15, 20, etc.
So far, I've mostly been writing code for problems that we humans can do in our head, but this is starting to get into the realm of problems that, while you can still solve in your head, you can not do so nearly as fast as a computer.
So let's write code for how you'd solve this:
First, let's consider the list of numbers you want to check, and let's say that the list is input_nums = [1,2,3,5,12,15,17,20]. You start by going over each item in that list, so for num in input_nums:.
Now, what do you do next? First, you need to express the condition: if the number is a multiple of 5. That definitely sounds like an if statement, but how do you actually express "multiple of 5" ?
Remember the modulo operator? The one that is written as a percentage (%) sign? This operator gives you the remainder of a division. A number is a multiple of 5 if, when dividing it by 5, you get the remainder 0. So you can write that as if num % 5 == 0:. This says if the remainder of dividing the current item in the list by 5 is 0, then let's print it: print(num).
Let's run this code, and you get 5, 15, and 20 which indeed are the only numbers that are multiples of 5 in the list above:
# Let's define our list
input_nums = [1,2,3,5,12,15,17,20]
# Now let's create our for loop with our if statement
for num in input_nums:
if num % 5 == 0:
print(num)
# Our output is: 5, 15, 20
How to Use For Loops with Strings
Let's do another problem.
Given a list of names, print the ones that begin with the letter a. Let's store this list in a variable, call it names, and let's set it to: names = ['Andrea', 'Jim', 'Beyonce', 'Batman'].
Totally random. So now, let's go over each name in this list: for name in names:. You want to only print the names that start with a. Perhaps you remember that, from strings, you can get the first character using its index position. So what you want to do is compare the character at index 0, which is the first character in the string, with 'A,' and, if they are equal, you want to print the name.
You do that by writing: if name[0] == 'A': print(name). If you run this code, you'll get Andrea, which is indeed the only name that starts with an 'A' in that list:
# Let's define our list
names = ['Andrea', 'Jim', 'Beyonce', 'Batman']
# Now let's write our code
for name in names:
if name[0] == 'A':
print(name)
# Our output is: Andrea
Let's make this problem a bit more interesting.
Let's say that, if the name does not start with an 'A,' you just want to print the letter it starts with. That should be pretty easy. You take all the logic you have so far, and you add an else branch to your if. This else branch will only get executed if the condition on the if branch is not True.
That is, it will only get executed if the name does not start with 'A.' So, in this case, you just print(name[0]) because name[0] is the first character in the current name.
Let's run this, and you get the full name Andrea, because it starts with an 'A,' and then you get J for Jim, B for Beyonce and B for Batman, because Jim, Beyonce, and Batman don't start with an 'A,' so you only output their first character:
# We'll keep our list
names = ['Andrea', 'Jim', 'Beyonce', 'Batman']
# Now, we'll update our code
for name in names:
if name[0] == 'A':
print(name)
else:
print(name[0])
# Our output is Andrea, J, B, B
So you see, this is where you are starting to get more power and are able to write more complex programs. I think this is where it becomes a matter of practice. The more code you write, the easier it will be for you to take a problem that is expressed in plain English and figure out what building blocks you need in order to express it in code. That ability only comes with practice, so the more you code, the easier it will be.
How to Use For Loops With Running Totals
Let’s go over some more hands-on practice with for loops and lists.
Let's say you have a stack of bills to pay, and you want to sum them up to figure out what our total bills for the month are. For simplicity, let's assume that you already have the amounts you owe for each bill, and you store those in a list variable.
Let's call that bills_list and say that you have 70 dollars for internet, 100 for our phone bill, 1000 for rent - that last one can be either a really good deal or a really bad deal, depending on the city you're in, of course - and let's say you also spent about 600 dollars on food.
Now, you want to sum those up to figure out how much you owe for those bare necessities of life.
Let's say you're going to store this total in a variable named total, and let's set that total to 0 initially. Next, you're going to go through each bill in your list of bills and add that bill to your running total. So you can write for bill in bills_list: total = the current total plus this bill, or total + bill.
So now, once the whole for loop finishes, the variable named total should store the sum of all the bills in bills_list. You can print it, so print(total), and when you run this code you get 1770. That seems about right: 1000 + 100 + 600 + 70 is indeed 1770.
# Let's define our list and our variable
bills_total = [70, 100, 1000, 600]
total = 0
# and create our for loop
for bill in bills_list:
total = total + bill
print(total)
# Our output is: 1770
Let's go over this again and make sure it all makes sense.
So, bills_list is just the list of bills. Total is the variable where you store the final total amount. You set it to 0 because you need to define it and give it an initial value first before you can use it. That's because in Python, you cannot use a variable before it is defined. You set it to 0 because 0 is neutral to addition. You can add 0 to any number and it won't change the result.
Next, the for loop should hopefully be pretty self-explanatory: you simply iterate over each of the bills in bills_list, and you use the variable named bill to refer to each of the bills in the list. In the first step of the loop, this variable bill gets assigned the integer number 70, which is our first bill.
Next, total, which is currently 0, gets added to 70, so total is now 70. So after the first step in the for loop, total is equal to 70.
On the second step of the loop, bill gets assigned the next value in the bills list, which is 100. Next, total, which on the previous step was updated to store the value 70, is added to 100 and you get 170. This new value is then assigned back to total. So after the second step, the value you store in total is now 170.
On the third step of the loop, the variable bill gets assigned the next value in the bills list, which is 1000. Next, total, which on the previous step was updated to store the value 170, is added to this new bill, which is 1000, and you get 1170. This new value is then assigned back to total. So after the third execution of the loop, the value you store in total is 1170.
Finally, on the fourth step of the loop, the variable bill gets assigned the last value in the bills list, which is 600. Next, total, which at this point is 1170, gets added to this bill, which is 600, and you get 1770. This new value is then assigned back to total. So after the fourth execution of the loop, the value you store in total is 1770.
At this point, the loop exits, which is another term you use to say that the loop ends. The next line in your program prints the current and final value stored in total, which is 1770.
Again, I want to stress how important the indentation is. Because the print function is not indented, it is not considered part of the for loop, so it won't run at each step of the loop. Instead it gets executed only once, when the loop ends.
Compare that with this: in this case, print has the same indentation as total = total + bill. So what's going to happen? The value you store in total is going to be updated as I discussed earlier, but in addition, this print statement is going to get run on every step of the loop.
If you run this code, you get 70, 170, 1170, and 1770. That's because each time you update the value stored in total, you also print it. Those values match the ones you deduced earlier.
# Let's define our list and our variable
bills_total = [70, 100, 1000, 600]
total = 0
# and create our for loop
for bill in bills_list:
total = total + bill
# but this time, let's indent our print function
print(total)
# Our output is 70, 170, 1170, 1770
In your programs, you will often use both of these strategies. Sometimes you'll want to print the running total as it gets incremented. Sometimes you'll just want to print the total at the end, once everything was added up. Two fairly different outcomes separated by a simple indentation. So pay attention to those white spaces.
Next, I will go over the concept of a nested for loop, that is, a loop within a loop.
How to Use For Loops in Nested Loops
On this occasion, you'll also move on to iterating over other kinds of iterable objects. I mentioned, for example, that strings are also iterable because they are nothing but sequences of characters. So let's take a string, say, name = 'James Bond.' Now, let's say you want to print each letter in the name three times. How do we do that? Well, whenever you hear words like “each," you know a loop is needed.
So you start with for letter in name:. This variable named letter will be assigned, in turn, to each of the characters in the string 'James Bond.'
How do you print a letter three times? Perhaps you remember that when I talked about strings, I mentioned that you can use the (*) operator, the same operator that is used for number multiplication. In this case, because the variable letter stores a string, it won't do number multiplication, but you can use it to print the letter string as many times as you want. So you just write here print(letter * 3).
And if you run this, you get the result you want. You can replace 3 here with 5, and now you see each letter printed 5 times. Notice that even the space gets printed 5 times, but being a space, you can't see it. It's there though, trust me.
# Let's create our string
name = 'James Bond'
# and our for loop
for letter in name:
print(letter * 3)
# Our output is JJJ, aaa, mmm, eee,
#sss, , BBB, ooo, nnn, ddd
Here's a more interesting problem. What if instead of a single name, you have a list of names, and, again, you want to print each letter of each of the names 3 times?
This is a bit trickier, but bear with me. Let's start with the code you had before.
First, instead of name, let's define a variable called names_list, and set it to the list ['James', 'Batman', 'Marie']. Now, if you go to the for loop and replace name with names_list and run it again, you get JamesJamesJames, followed by BatmanBatmanBatman, followed by MarieMarieMarie.
# Let's try adjusting our string
names_list = ['James', 'Batman', 'Marie']
# and adjust our for loop
for letter in names_list:
print(letter * 3)
# Our output is JamesJamesJames,
#BatmanBatmanBatman, MarieMarieMarie
So not quite what you want. Remember, you want each letter printed 3 times for each of these names, not the names themselves printed three times. So, how do you do that?
Here's a silly way of doing it. You can just copy-paste the loop you had before and, instead of name, replace it in each instance with each of the names. So you end up having three loops that pretty much look the same, except that in each case you're iterating through a different name.
For instance, this works: each letter in the name James is printed 3 times, and underneath each letter in the name Batman gets printed 3 times, and finally, each letter in the name Marie gets printed 3 times.
What's the problem with it though?
# Let's use our same list
names_list = ['James', 'Batman', 'Marie']
#and create the 3 for loops
for letter in 'James':
print(letter * 3)
for letter in 'Batman':
print(letter * 3)
for letter in 'Marie':
print(letter * 3)
# Our output will be a long list: JJJ, aaa, mmm, eee, sss,
# BBB, aaa, ttt, mmm, aaa, nnn, MMM, aaa, rrr, iii, eee
Perhaps you're thinking I'm too lazy and I don't want to type that much, and that's actually a good instinct.
When you program, in most cases, it pays off to write as little code as you can get away with. Less code written means less code that needs to be read, less code that needs to be updated, fewer opportunities for typos or other bugs. Besides, what if we had a list of 100 names instead of 3? I'm definitely not typing that.
So what's a different way to get this result? Well, look at these three for loops. What you're doing here is basically the same thing three times. You're just doing that thing to a different item. Doesn't that look like - a loop? If that was your guess, you are correct.
Every time you repeat very similar steps like these, you can wrap what you're doing in a loop. So you can solve this problem with a loop nested inside another loop.
In the outer loop, you go over each of the names in our names_list and assign it, in turn, to a name variable. Then, inside that loop, you have another loop that goes over each letter in the name and prints it 3 times. So let's write that and see what it looks like.
Let's delete these for loops and write for name in name_list: print(name). If you run this, you see that you print each name, as you'd expect. Now, instead of print(name), let's copy the loop you had before, so you replace it with for letter in name: print(letter * 3). Notice that the second for is indented 4 spaces, or 1 tab character, just like the print statement before.
Also, the print statement inside the second for loop is indented 4 spaces or 1 tab relative to the second for keyword.
# Let's keep our list
names_list = ['James', 'Batman', 'Marie']
# and adjust our for loop
for name in names_list:
for letter in name:
print(letter * 3)
# Now, our output is JJJ, aaa, mmm, eee, sss,
# BBB, aaa, ttt, mmm, aaa, nnn, MMM, aaa, rrr, iii, eee
This is crazy! You have a loop inside a loop. This whole second loop gets executed for each name in the names list. If you run this, you'll get what you want: each letter in the name James is printed 3 times, and underneath, each letter in the name Batman gets printed 3 times and finally, each letter in the name Marie gets printed 3 times.
You will often need to use these nested loops, so I wanted to show you an example of how you can use them. You can nest as many loops as you need, but I've rarely seen them nested more than 3 or 4 times because, beyond that level of nesting, it becomes very hard to reason through them. However, this single nesting is very common, so definitely spend some time and make sure you understand it fully.
How to Use Nested For Loops with Nested Lists
Here's another very common use for nested loops. Let's say you have a list called nums_list, short for numbers list, and it consists of three nested list. Each one of those nested lists has numbers inside it: nums_list = [[1,2,3], [4,5,6], [7,8,9]]. You can certainly do this because lists are very flexible and can hold other lists inside them. The tricky question is, how do you print each of the numbers you have here on a separate line?
Perhaps your first approach is to write something like for list_item in num_lists: print(list_item). However, if you run this, you see that the output is not what you want it to be. Why is that?
# Let's set our list
nums_list = [[1,2,3], [4,5,6], [7,8,9]]
# and create our for loop
for list_item in nums_list:
print(list_item)
# Our output is [1, 2, 3], [4, 5, 6], [7, 8, 9]
Well, the code does indeed do what you tell it to do. It goes over each item in the list and prints it out. But each of those items is itself a list, not a number. That's because nums_list has only 3 items: the list [1,2,3] is the first item followed by a comma, followed by the list [4,5,6], which is the second item, followed by another comma, and, finally, followed by the list [7,8,9], which is the third item.
Python looks at your code and says, "ah, you want me to print the items in your nums_list, here they are." It's these three lists. This can be a bit tricky perhaps to see at first: Python only looks at the top level when iterating through an object, it does not iterate inside the nested objects by default.
But how do you get to the numbers inside the nested lists? It looks like for each one of those nested lists, you further need to iterate and print their own items. So you can change that code and write for list_item in nums_list: for num in list_item: print(num).
Notice again the indentation. The second loop is inside the first loop, so you need the tab there (or 4 spaces if you don't like tab). If you run this code, you get what you want. How does that work though?
# We'll keep the same list
nums_list = [[1,2,3], [4,5,6], [7,8,9]]
# and use our nested for loop
for list_item in nums_list:
for num in list_item:
print(num)
# Our output here is 1, 2, 3, 4, 5, 6, 7, 8, 9
Well, you saw earlier that the variable list_item in turn gets assigned to the list [1,2,3], then the list [4,5,6], and finally the list [7,8,9] in the outer loop. Now, in the inner loop, you iterate over each one of these items and you print each of their elements. So the outer loop begins by assigning the variable item_list to be the list [1,2,3], which is the first item in our nums_list. Then you execute the inner loop, and if you iterate over that, you'll get the integer 1, then the integer 2, then the integer 3, because those are the elements that are in it.
Then, the inner loop ends and you're back in the outer loop. Now, item_list gets assigned the next value, which is the list [4,5,6] and then you again enter the inner loop and you iterate over each of the elements in [4,5,6].
When you print each of them, you get the integer 4, then the integer 5 and then the integer 6. Then the inner loop again ends and you're back in the outer loop. At this point, the outer loop assigns item_list to be the list [7,8,9], which is the last item in our nums_list.
Finally, you enter the inner loop for the third time, and you iterate through the list [7,8,9] and print each item to finally get the integers 7, 8, and 9.
Play with these for a while. They might seem a bit difficult at times, and perhaps they remind you a bit of the movie Inception. It's indeed a bit like that. A loop inside a loop.
As I said, it can get a bit more complex, and you can have a loop inside a loop inside a loop. I won't go over those examples right now, but the idea is exactly the same. Now, I'll look at how you can use for loops with two other Python iterables. Namely, tuples and dictionaries.
How to Use For Loops with Tuples
Let's talk about tuples. I won't go into as much detail with tuples, because everything that I talked about when I discussed lists or strings also applies to tuples. Let's just write an example.
Let's say I have a tuple called coins_list = (1,5,10,25). So that's a tuple that stores the coin values that are in wide circulation in the United States. Let's quickly iterate through that list and, to make it more interesting, let's print out how many of each kind of coin you need to get to 1 dollar.
So you can write for coin in coins_list: print(100/coin) print(coin). That works because 1 dollar is 100 cents, so, for example, if you want to know how many 5-cent coins you need to make a dollar, you just divide 100 by 5 and you'd get 20.
So, if you run this code, you see that we need 100 1-cent coins to make a dollar, 20 5-cent coins, 10 10-cent coins, and finally, only 4 25-cent coins.
# Let's create our tuple
coins_list = (1, 5, 10, 25)
# Now let's create our for loop
for coin in coins_list:
print(100/coin)
print(coin)
# Our output is: 100.0, 1, 20.0, 5, 10.0, 10, 4.0, 25
How to Use For Loops with Dictionaries
Finally, let's iterate over some dictionaries also.
Let's say you have a dictionary that gives us fruit prices: market_prices = {'apples': 2.99, 'oranges': 3.99, 'avocados': 4.99}. Now, you can write: for item in market_prices: print(item). If you run this code, you get apples, oranges, and avocados.
Why is that? That's because, by default, when it comes to dictionaries, Python iterates through the list of keys, not through the list of pairs. It can be a bit confusing, but that's just how it works. If you want to iterate through the list of pairs, you need to change the loop to be for item in market_prices.items(): print(item).
When you run this, you get the pairs:
# Let's create our dictionary
market_prices = {'apples': 2.99, 'oranges': 3,99, 'avocados': 4.99}
# and create a for loop
for item in market_prices:
print(item)
# Our output is apples, oranges, avocados
# Let's adjust our for loop
for item in market_prices.items():
print(item)
# Now our output is: ('apples, 2.99), ('oranges', 3.99), ('avocados', 4.99)
You can also iterate through the list of values by changing the code to be for item in market_prices.values(): print(item).
You learned about these functions item() and values() in earlier articles where I reviewed the dictionary data type.
# Let's keep our dictionary
market_prices = {'apples': 2.99, 'oranges': 3.99, 'avocados': 4.99}
# and adjust our loop to show the values
for item in market_prices.values():
print(item)
# Now our output is 2.99, 3.99, 4.99
I want to show you one more thing.
You might think that it's a bit silly that, by default, iterating through Python dictionaries iterates through the keys, but it actually makes sense. Remember that, in a dictionary, you access values using their keys. So, let's go back to the first loop I wrote here, for item in market_prices: print(item). I'll run that again, and you can see that it prints the keys.
Well, how can you access the values that correspond to each of the key? Simple. Instead of print(item) here, you can write print(market_prices[item]). If you run this, you see that you actually get the values. This works because in each step of the loop, this variable named item is assigned to some key in the dictionary, and, below, you use market_prices[item] to access the value that corresponds to that key.
# Our dictionary
market_prices = {'apples': 2.99, 'oranges': 3.99, 'avocados': 4.99}
# and our for loop that shows the keys
for item in market_prices:
print(item)
# This returns apples, oranges, avocados
# Another way to see our values
for item in market_prices:
print(market_prices[item])
# This will return 2.99, 3.99, 4.99
Remember, you need to be careful with dictionaries, because they are unsorted sequences, the order in which you get the keys is not guaranteed. But you'll definitely get all of them.
While you can use for loops with just about every type of iterable in Python, they are most commonly used with lists, and particularly with lists of numbers. Now, let’s explore a more convenient way to generate a list of numbers: the range function.
How to Use For Loops and the Range Function
You'll talk more about functions in a bit, so I won't go into too much detail about what a function is just now. Instead, I just want to show you how you can use range with loops. Primarily, let's consider this use case:
I want to write the text 'hello' 5 times. How do I do this? Well, as you've seen before, you could just write print('hello') five times, but that would be silly because you actually know how to use loops. So instead, you can write this: for i in [0,1,2,3,4]: print('hello').
And this works. If I run it, I see the word 'hello' printed 5 times. I started counting at 0 here, but there are 5 elements in that list and the word 'hello' is printed for each of them, so it's printed 5 times.
# Let's create our for loop
for i in [0,1,2,3,4]:
print('hello')
# Our output is hello, hello, hello, hello, hello
Ok, but what if I now have to print the word 'hello' 10 times, or 100 times? I guess I could just write all the numbers from 0 to 9, or to 99, but there has to be a better way to do that. And indeed there is.
You can just use the range function like so: for i in range(9): print('hello'). If you run this, you see that the word 'hello' was printed 9 times. How does that work?
Well, the range function returns a list of numbers - in our case, it generates 9 numbers, starting at 0 and ending at 8. You can also replace our earlier code with: for i in range(5): print('hello'). If you run it again, you see the word 'hello' printed 5 times.
There are more advanced uses of the range function too, but for now, remember that you can just use it like this, to repeat an action a number of times.
# Let's use the range function
for i in range(9):
print('hello')
# Our output here is hello, hello, hello, hello,
#hello, hello, hello, hello, hello
# Let's try it again
for i in range(5):
print('hello')
# And now we get hello, hello, hello, hello, hello
This concludes our foray into Python for loops. It's quite a bit of material if you're new to it, so take your time, re-read these segments if you need to, and practice, practice, practice, to get comfortable with loops.
Now that you know if statements and for loops, and you covered the main data types in Python, you can write programs that solve much more complex problems, as you've seen.