How to break out from multiple loops python?
source link: https://thispointer.com/how-to-break-out-from-multiple-loops-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In this Python tutorial, you will learn how to break out of multiple loops in Python.
Agenda
Let’s dive into the tutorial.
Introduction
We have to stop at some point in a looping statements. Based on the condition or requirement in the application, we have to come out of the loop. In python, we can do this by using break and return keywords.
Let’s study it in detail.
Advertisements
break:
The break is a keyword in python and it used to break out from a loop. It will stop the execution of looping statements based on the condition specified under a loop.
return:
The return is a keyword in python used to break out from the function. It is used only with functions. Based on the condition, it will return the result and terminate working of the function. The main thing is that we have to provide for loop within the function.
Break out from multiple for loops using break
Here, we will use nested for loops and specify the condition to break using the if statement. So based on the condition, we are going to break from both the for loops.
Syntax:
for iterator1 in iterable/range(sequence): for iterator2 in iterable/range(sequence): ............... ............... for iteratorn in iterable/range(sequence): conditional-statements break from all loops other statements ............... ...............
Here, an iterator is used to iterate the looping statements and conditional statements refer to if-else, etc. We need to break the loops by checking conditions.
Let’ ‘s see a few examples to understand it better.
Example 1:
In this example, we are using two for loops to iterate two lists of integer elements and break the loops if the sum of two iterators is greater than or equal to 10. Otherwise, we are displaying the two iterators.
#create list of 5 integers list1=[6,7,8,9,10] #create list of 3 integers list2=[2,4,6] breakFlag = False #iterate first list for iterator1 in list1: #iterate second list for iterator2 in list2: #check if sum of two iterators is greater than or equal to 10 if(iterator1+iterator2>=10): # break the loop breakFlag = True break else: #display iterators print(iterator1,iterator2) #break first loop if breakFlag: break
Output:
6 2
The first list has 5 integers, and the second list has 3 integers. We created a variable breakFlag which is initialied to False. We can see that if the sum of two iterators is greater than or equal to 10, it goes inside if condition and breaks the second or inside loop by executing the break statement. So again we are checking the breakFlag status, if it is True, then we are breaking the first loop. In other cases, it goes inside else block and displays the iterators.
We can also use the break inside the function. Let’ ‘s see the same example to understand it better.
Example 2:
# Create a function named - my_ownfunction def my_ownfunction(): #create list of 5 integers list1=[6,7,8,9,10] #create list of 3 integers list2=[2,4,6] breakFlag = False #iterate first list for iterator1 in list1: #iterate second list for iterator2 in list2: # check if sum of two iterators is # greater than or equal to 10 if(iterator1+iterator2>=10): # break the nested loop breakFlag = True break else: # display iterators print(iterator1,iterator2) # break the first loop if breakFlag: break # call the function my_ownfunction()
Output:
6 2
It is the same as the first example, but we have written all the code inside the function named – my_ownfunction() and called it.
Break out from multiple for loops using return keyword
The return keyword can be used within functions. It returns a value and will skip the statements after the return statement. We can also say that it is used to exit a function.
Syntax:
def function_name(): for iterator1 in iterable/range(sequence): for iterator2 in iterable/range(sequence): ............... ............... for iteratorn in iterable/range(sequence): conditional-statements return function_name() other statements ............... ...............
where function_name() is the name of the function and at last we have to call the function by its name.
Example 1:
In this example, we are using two for loops to iterate two lists of integer elements and break the loops by returning the sum of the first iteration.
def my_ownfunction(): #create list of 5 integers list1=[6,7,8,9,10] #create list of 3 integers list2=[2,4,6] #iterate first list for iterator1 in list1: #iterate second list for iterator2 in list2: #return the sum of first iteration return iterator1 + iterator2 #call the function value = my_ownfunction() print(value)
Output:
8
From the above code, we have seen that the first 6 from the first list and 2 from the second list are iterated. The sum of 6 and 2 is 8 and we are returning this.
Example 2:
In this example, we are using two for loops to iterate two lists of integer elements and break the loops by checking the condition that the sum of two iterators that are greater than 8.
def my_ownfunction(): #create list of 5 integers list1=[6,7,8,9,10] #create list of 3 integers list2=[2,4,6] #iterate first list for iterator1 in list1: #iterate second list for iterator2 in list2: # If sum of both the numbers is greater than 8 # then exit from both the loops and return from function if iterator1 + iterator2 > 8: return iterator1 + iterator2 #call the function value = my_ownfunction() print(value)
Output:
From the above code, we have seen that the first 6 from the first list and 4 from the second list are iterated, so the sum of 6 and 4 is 10. Hence the condition satisfies which is greater than 8. so it is returned the and broke both the loops.
Summary
If you want to break out from multiple loops in a function, you can use return as well as break. If your application doesn’t contain any function and you want to break out from multiple loops, you can use the break keyword. But make sure that return is used only with in the function.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK