This tutorial is about how to print and perform operations on a list using python for a loop. Lists are the built-in data types that can store multiple objects at the same time just like arrays. In this tutorial, we’ll learn about the different ways to iterate over the lists. First of all, create a list. To access any element, we need to use its index number. Suppose we want to access the third element in list ‘a’. Let’s see an example of the Python For Loop list.
If you want to learn more about for loops and lists in Python, See Python Tutorials.
Although we can access list elements through their index numbers, this method is inefficient when we want to apply some operation on all elements of the list. For this, we need to iterate over the elements of lists and we can do this by using for loop. You’re already familiar with the syntax of for loop. There are different ways to access all elements of lists one-by-one using for loop. Lets discuss them one by one.
Using Simple for loop
It is the most basic and easiest method. The syntax of for loop is:
#Syntax of for loop for element in list: #Statements
On each iteration, an item from the list is assigned to the “element” variable starting from the first index to the last index.
In the above example, we have initialized a list ‘a’ with five elements. Then, we have initialized a for loop which will iterate over the list one by one and print the elements using a print() command.
using Range() function
We can also use a range() function along with for loop to traverse through a list. The syntax of range() function is shown in the code snippet. It takes three parameters as an input. The start and step are optional and are set to 0 and 1 by default whereas the ‘stop’ argument is mandatory. It specifies the ending position. The range() command when executed returns a sequence of numbers starting from 0 (by default) with a step size of 1 up to the number specified by the user. For instance, if we execute range(5), it will return numbers from 0 to 4.
range(start, stop, step)
In the above example, we have passed the length of the list in the ‘stop’ parameter of the range function. Here, the len() function calculates the length on the list. As in this example, the list of the length is 7, therefore the range function will return a sequence of numbers from 0 to 6. The loop will run seven times and on every iteration, the value of x variable increments by 1 which is then passed as an index to print the list elements. Instead of calculating the length of a list separately and then passing it to a range function, you can also directly pass the length to the range function. The output would be the same.
Using enumerate() function
In some applications, you may want to get the index of each item as well. For this, we have a built-in function called enumerate. The enumerate() will return an enumerator object which is iterable in each iteration. It will return a tuple of each element and its index.
You can observe from the output of example 3, the loop returns a tuple of items and their index on every iteration. We can also unpack the tuple inside for loop to obtain the two items i.e., index and the element.
For any questions related to Python for loop list Contact Us