Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Nested While Loops

Give Feedback

In this lesson you’ll learn how to nest multiple while-loops and when it can be helpful. Here’s a simple example of nested while-loops, which is debugged in the video:

a = ["fizz", "buzz", "baz"]
while a:
    print(a.pop(-1))
    b = ["---", "==="]
    while b:
        print(b.pop(-1))

00:00 So while we are looking at this example, there’s actually one more thing we can talk about, and that’s the idea of having these nested conditional statements.

00:08 So, we have our while loop out here and inside of it, we have nested another conditional statement, an if statement. And we saw how those two things work together.

00:18 Now, you can do these with all sorts of combinations. You could have the if statement on the outside and while loops on the inside, you could have a combination of both on the inside.

00:28 They can get pretty complicated if you want them to or if you need them to. However, I just want to show you one more example, looking at a while loop nested inside of another while loop. And to do that, we’re going to go back to an example that we’ve used before and say that a is actually a list containing three things.

00:49 a is going to contain 'fizz', 'buzz', and 'baz'. And then I’ll have my while loop, while a: I’m just going to print the result of popping off the last element in that list.

01:07 But then I’m going to initialize another variable, so I’m going initialize another list, b, and I’m going to set that list to two elements just to make our code nice and pretty.

01:20 And then I’m going to write another while loop nested inside of my original one. So I have my original while loop, and then I’m going to nest my second while loop. And in this second loop, I’m going to do the same thing.

01:33 I’m going to print the result of popping off the last element in b. So, let’s see what happens. All right, so we can see over here both my lists are empty at the end of this and I have my baz, my buzz, and my fizz.

01:51 But every time that I printed off that first thing, I kept my two elements the same from my list b. Let’s look at the debugger and actually see why that happened.

02:06 We have our list like we did initially, and then I’m going to start my while loop. I have elements in a, so it’s going to return True,

02:25 pop off that last element, so it prints baz. Then I see that second variable

02:36 and then I jump into my second while loop.

02:47 So, keep an eye over here. You’ll notice after that first while loop, I have gotten rid of that first element, or that first iteration. And now it’s empty.

03:01 However, once that’s done, it jumps back up to a and I initialize that variable again,

03:13 and now I have buzz. Then I’m going to continue on into my second while loop, right? And it’s now initialized again, because it is nested inside of this, outside our first while loop.

03:30 So this second variable is actually reinitialized because I have it inside here. So, I can continue on all the way through the end

03:46 and have that result. Now, as I mentioned, these can be really simple, but most of the time you’ll see them pretty complicated. You may want to do this whenever you have many conditions that need to be met, or depending on a certain condition you want to execute a certain block of code. But, once again, this is when some of your code can get a little more complex.

04:11 We have one more thing to talk about, and that is an actually more simple while loop, which is the one-line while loop.

Become a Member to join the conversation.