For-Else: A Weird but Useful Feature in Python

Use it properly and skilfully

Yang Zhou
Yang Zhou
Apr 18 · 3 min read

No matter which programming language we use, we all write “if-else” statements, but how about “for-else” ?

For many languages, such as C, C++ and Java, it’s totally wrong if you use an “else” after a loop. However, Python, as an elegant language, has this weird but useful feature. If we use it properly, our code will become neater and cleaner.

This article will introduce the “for-else” feature in Python and explain how to use it properly by simple examples.

The Basics of the “For-Else” Feature

The “for-else” feature looks strange and hard to understand when a Python developer encounters it for the first time. But in fact, its usage is unexpectedly simple. One sentence is enough to explain it:

The “else” block only executes when there is no break in the loop.

Too simple to be true? Let’s check it out by an example:

As shown above, the list leaders doesn’t contain “Yang”, so there is no break in the for loop. Therefore, the “else” block executed and printed the information.

What will happen if the leaders list includes “Yang”?

As shown above, since “Yang” is in the leaders list, the for loop was broken and the “else” block didn’t execute.

In a nutshell, the for-else feature is not too hard to understand. But using it properly and skilfully is not easy. There are at least three scenarios suitable for the for-else feature.

3 Scenarios To Use the For-Else Feature

It’s not necessarily that we must use the for-else feature in our Python programs. To be honest, we can do the same things without it, but using it can make our code more elegant.

1. Iterate and find items without a flag variable

Iterating a list to find a specific item is a basic scenario to use a loop. Usually, when we find the item, it makes no sense to continue the iteration and we need to break out of the loop. The problem is: how can we know whether the item was found or not?

A traditional solution is to define a “flag” variable and set it to True when we found the specific item.

As the above example shows, the have_yang variable works like a flag. After the for loop, if it is False, we know there is no “Yang” in the list and we can do some other operations.

This method is good enough, but if you want to take advantage of the beauty of Python. Using the for-else feature is another option:

A little neater and cleaner, right?

2. Help to break out of nested loops

When there are nested loops, the for-else feature can help as well.

As shown above, breaking out of the nested loops is a little harder, cause we have to know whether the inner loop was broken or not.

The above code shows a clumsy solution to determine if the inner loop has broken out or not. It works of course, but we can make it neater by the for-else feature:

3. Help to handle exceptions

Exceptions handling is very important for programming, and if we use the for-else feature properly, it can be helpful as well. Such as the following example:

As shown above, we can do the corresponding operations in the “else” block if there is no ZeroDivisionError in the for loop.

Conclusion

The for-else feature in Python seems weird at first. But it’s not hard to understand and can be very useful for some scenarios. After all, we only need to remember the one-sentence rule:

The “else” block only executes when there is no break in the loop.

Thanks for reading. If you like it, don’t forget to follow me to get more great articles about programming and technologies!

Relative articles:

TechToFreedom

Dive into technology, investment and entrepreneurship.

Yang Zhou

Written by

Yang Zhou

Software Engineer | Investor | Entrepreneur yangzhou1993.medium.com/follow

TechToFreedom

Technology should and must give each individual greater freedom.

Yang Zhou

Written by

Yang Zhou

Software Engineer | Investor | Entrepreneur yangzhou1993.medium.com/follow

TechToFreedom

Technology should and must give each individual greater freedom.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store