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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Using return to Short-Circuit Your Loops

00:00 Now you’ll see how return statements can speed up the execution of functions which use loops. When a Python function encounters a return statement inside a loop, it immediately ends execution of the loop and the function and returns to where the function was called.

00:18 This is referred to as short-circuiting. We short-circuit the execution of the loop. There are many advantages to this, and we’ll explore the most common one here.

00:28 Consider a function to search a collection for a specific item. Once the item has been found, there’s no need to keep searching for it in the collection.

00:37 You always find your keys in the last place you look—if you don’t keep looking for them after you found them. We don’t need to process the rest of the collection, which improves the execution time of the function.

00:51 An example of this is found in Python’s built-in function any(). It returns True if any of the items in the collection passed as a parameter are truthy. What might this look like?

01:05 Here’s one possible implementation, and we’ll call it my_any() to distinguish it from Python’s built-in function. The argument can be any iterable type, so we’ll call it iterable.

01:18 As per usual, we’ll process it one element at a time using a for loop, calling each element item. If at any iteration, that specific item has a truthy value, we can go ahead and return True. If we know that there’s one in that collection, the return value for this function should be True.

01:41 If we completely process the collection and complete the execution of the for loop, then there must not have been any element in the collection with a truthy value, so in that case, we’ll return False to indicate that there weren’t any.

01:57 Let’s try this out.

02:04 Let’s call my_any() on a real simple collection, [0, 0, 1, 0, 0]. This does have at least one element with a truthy value, and this returns True.

02:23 If I give it a collection without any elements having a truthy value, it will return False. Let’s actually modify our function so we can see the iterations happening.

02:37 Let’s add a print statement before we check the truthiness of each item. Let’s save this, and restart our interpreter,

02:55 and re-import it.

03:04 This time, we will see the values print just before we test them.

03:10 You can see we only went through three iterations of this loop. We checked the 0, False. 0? False. 1?

03:20 True. We didn’t bother looking at the remaining elements. We knew we had at least one that was True. That’s all we needed to know.

03:30 On the other hand, if we provided a collection without any truthy values in it, we’ll see it processing all five of the zeros, not finding anything with a truthy value, so it returns False.

03:44 And again, this behavior is similar to Python’s built-in any() function. Remember that the return statement needs to be inside an if block if it’s inside a loop.

03:56 Otherwise, the function will return during the first iteration, which is probably not what you want. The remaining code will never be executed. That remaining code is an example of what we call dead code, and we’ll look at that in the next lesson.

Become a Member to join the conversation.