How to Loop with Indexes in Python

Posted in /  

How to Loop with Indexes in Python
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Python is one of the most popular high-level programming languages. Like other programming languages, Python supports iterative or loop control flow such as for and while loop. If you have prior knowledge of other programming languages like C, C++, and Java, you can clearly see the difference between for loop in Python and other programming languages. In Python, we can directly use the for loop to iterate over the Python list or Python array.

    However, in other programming languages, we have to use the index value to iterate through an array. In this article, you will learn how Python for loops are different from for loops in C++, C, and Java, and how to loop with indexes in Python using for and while loop. So, let's get started!

    For Loop in C++, C, and Java

    In C++, C, and Java, the syntax of for loop is the same, the for loop statement is divided into 3 parts, namely initialization, comparison, and increment or decrement.

    Syntax for(initialization; comparison; increment or decrement)

    Example

    #include <iostream>
    using namespace std;
    int main()
    {  
       int arr[5] = {10,20,30,40,50};
       int len = sizeof(arr)/sizeof(arr[0]);
        for(int i=0; i<len; i++)
        {
            cout<<arr[i]<<endl;
        }
        return 0;
    }

    In the above example, you can see that by using the for loop, we can only create a sequence of integer values and we can use those integer values as indexes for the array.

    Looping in Python

    Now, let’s discuss looping or iterative statements of Python. In Python, we have two reserved keywords to create looping statements, namely for and while . The working and implementation of Python while loop is similar to the while loop in other programming languages, but the implementation and working of the for loop in Python loop is different. In other programming languages, we have foreach loop, which works similarly like the for loop in Python.

    Python for Loop

    Python for loop work along with the Python in operator and an iterable object. The for loop iterate through every single element of the iterable object till the end. The working of for loop in Python is different because we can traverse through a Python list or array without requiring the index value.

    Example

    numbers = ["one", "two", "three", "four", "five"]
    
    for number in numbers:
        print(number)

    Output

    one
    two
    three
    four
    five

    Python While Loop

    The while loop of Python is similar to the while loop in other high-level programming languages . If we want to traverse through a Python list or array using the while loop, we need the index value for every element, because with while loop we can not directly traverse through the list or array.

    Example

    numbers = ["one", "two", "three", "four", "five"]
    
    index = 0
    
    while index < len(numbers):
        print(numbers[index])
        index+=1

    How to Iterate with Indexes in Python?

    Sometimes when we traverse through a Python iterable object using for loop, we may require the index value alongside the element value. In that case, we can use one of these below techniques.

    1. Using the range() method with len()

    range() is a Python generator method, which is often used along with the for loop when we want to iterate over a range of integer values. The range method accepts an integer value n and creates a generator of integer sequence from 0 to n-1 . Using the range() method, we can grab the index as well as the corresponding element value of our list.

    Example

    numbers = ["one", "two", "three", "four", "five", "six", "seven"]
    
    for index in range(len(numbers)):
        print(index, ":->", numbers[index])

    Output

    0 :-> one
    1 :-> two
    2 :-> three
    3 :-> four
    4 :-> five
    5 :-> six
    6 :-> seven

    2. Use enumerate method

    enumerate() is a built-in method in Python that can accept any iterable object, like Python list, string, tuple, set, and dictionary, and return an iterable object of tuple pairs containing iterable index number and value. With the help of enumerate() method, we can also iterate over a list and can access index values.

    Example

    numbers = ["one", "two", "three", "four", "five", "six", "seven"]
    
    for index, number in enumerate(numbers):
        print(index, ":->", number)

    Output

    0 :-> one
    1 :-> two
    2 :-> three
    3 :-> four
    4 :-> five
    5 :-> six
    6 :-> seven

    3. Use zip method with range() function

    The zip() method accepts two iterable objects and returns a zip object, which is an iterator of tuples pairs with pairing elements of both the iterable objects. We can zip the list elements with the range() function and can have tuple pairs of elements with their corresponding index values.

    Example

    numbers = ["one", "two", "three", "four", "five", "six", "seven"]
    
    for index, number in zip(range(len(numbers)), numbers):
        print(index, ":->", number)

    Output

    0 :-> one
    1 :-> two
    2 :-> three
    3 :-> four
    4 :-> five
    5 :-> six
    6 :-> seven

    Conclusion

    In this Python tutorial, we learned how we can iterate through a list using for loop and indexes in Python. In Python programming, you will be using the range function if you want to grab the index value for the list elements. The use of zip, and enumerate methods just make the program more complicated. We use for loop in Python when we are certain about the number of iterations. However, when we are not certain about the number of iterations, we need to use the while loop.

    Leave a Comment on this Post

    0 Comments