How to Check End of For Loop in Python Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Python, we often use loops to iterate over a collection like list or string. But sometimes, we may want to know when the loop has reached its end. There are different ways to check for the end of a for loop. Let's go through some simple methods to do this.Using else with For Loop (Most Efficient)The simplest way to check when a for loop has ended is by using the else clause. The else block after a for loop runs only when the loop has finished all iterations without being interrupted by a break statement. Python # Loop through numbers from 0 to 2 (range(3) generates 0, 1, 2) for a in range(3): print(a) else: print("End of loop") Output0 1 2 End of loop Other techniques that give us different levels of control and flexibility depending on the needs of our code are:Table of ContentUsing enumerate() to Track IterationUsing For Loop CounterUsing enumerate() to Track IterationSometimes we need to know the current index of the loop and also check if it's the last iteration. We can use the enumerate() function to get both the index and the value. Python for a, b in enumerate([10, 20, 30]): # Check if the current index 'a' is the last index (i.e., len(list) - 1) if a == len([10, 20, 30]) - 1: print("Last iteration") print(b) Output10 20 Last iteration 30 Using For Loop CounterIf we want even more control over the loop, we can use a counter to track iterations manually. Using For loop method is more complex but gives flexibility. Python a = [100, 200, 300] #Assign '0' to Counter variable counter = 0 for b in a: # Increment the counter by 1 for each iteration counter += 1 if counter == len(a): print("End of loop") print(b) Output100 200 End of loop 300 Comment More infoAdvertise with us Next Article How to Check End of For Loop in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs python Practice Tags : pythonpython Similar Reads Check end of file in Python In Python, checking the end of a file is easy and can be done using different methods. One of the simplest ways to check the end of a file is by reading the file's content in chunks. When read() method reaches the end, it returns an empty string.Pythonf = open("file.txt", "r") # Read the entire cont 2 min read How to Break out of multiple loops in Python ? In this article, we will see how to break out of multiple loops in Python. For example, we are given a list of lists arr and an integer x. The task is to iterate through each nested list in order and keep displaying the elements until an element equal to x is found. If such an element is found, an a 6 min read How to start a for loop at 1 - Python Python for loops range typically starts at 0. However, there are cases where starting the loop at 1 is more intuitive or necessary such as when dealing with 1-based indexing in mathematics or user-facing applications.Let's see some methods to start a for loop at 1 in Python.Using range() with a Star 2 min read How to Emulate a Do-while loop in Python? We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th 3 min read How to Check if Tuple is empty in Python ? A Tuple is an immutable sequence, often used for grouping data. You need to check if a tuple is empty before performing operations. Checking if a tuple is empty is straightforward and can be done in multiple ways. Using the built-in len() will return the number of elements in a tuple and if the tupl 2 min read Like