Built with Tutify.io

    AIStudyLabs

    For Loop Enumerate in Python

    For Loop Enumerate in Python

    In Python enumerate() function is used to get the counter values in a for loop. This is the best method when we want to access each value of a given iterable object with the counter. for loop in Python is a control flow statement that is used to execute code repeatedly over a sequence like a string, list, tuple, set, range, or dictionary(dict) type.

    The enumerate() function allows iterable objects and returns an enumerate object in the form of a tuple containing the index and the value. In this article, I will explain Python enumerate() function and using this syntax and parameters how we can count the values in a for loop with examples.

    <a id="h-1-quick-examples-of-python-for-loop-enumerate"></a>

    1. Quick Examples of Python For Loop Enumerate()

    Following are quick examples of Python For Loop Enumerate().

    
    # Below are the quick examples
    
    # Example 1: Using enumerate() to list
    courses=['java','python','pandas','sparks']
    new_list = enumerate(courses)
    
    # example 2: Using enumerate() to list with specified starting index
    courses=['java','python','pandas','sparks']
    new_list = enumerate(courses, 10)
    
    # Example 3: Using enumerate() to get the count in a for loop
    courses=['java','python','pandas','sparks']
    for value in enumerate(courses):
    
    # Example 4: Using enumerate() to set the starting count
    courses=['java','python','pandas','sparks']
    for value in enumerate(courses, start = 1):
    

    2. Syntax of enumerate()

    Following is a syntax of enumerate() function.

    
    # Syntax of enumerate()
    enumerate(iterable_object, start=0)
    

    <a id="h-2-1-parameters"></a>

    2.1 Parameters

    It allows two parameters.

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    It returns enumerate object.

    <a id="h-3-usage-of-enumerate"></a>

    3. Usage of Enumerate()

    Enumerate() function is a built-in function provided by Python. It takes an iterable object and returns enumerate object. This function automatically returns the counter(index) of each value present in the enumerated list. By default, the counter/index value starts from '0'. If we want to specify the starting counter we can use the start parameter as an optional parameter of enumerate() function.

    Apply Python Enumerate() function to the list, it will add a counter to each value of a list and return an enumerate object then, with help of the list() function we can get the list of tuple values.

    
    # Using enumerate() to list
    courses=['java','python','pandas','sparks']
    new_list = enumerate(courses)
    print(list(new_list))
    
    # Output:
    # [(0, 'java'), (1, 'python'), (2, 'pandas'), (3, 'sparks')]
    

    <a id="h-3-1-enumerate-with-customized-starting-index"></a>

    3.1 Enumerate with Customized Starting Index

    When we set start param as a specified value and pass it into enumerate() function along with iterable object. It will return each value present in the enumerated list with corresponding counter value. Here, counter value start from '10'.

    
    # Using enumerate() to list with specified starting index
    courses=['java','python','pandas','sparks']
    new_list = enumerate(courses, 10)
    print(list(new_list))
    
    # Output:
    # [(10, 'java'), (11, 'python'), (12, 'pandas'), (13, 'sparks')
    

    <a id="h-4-use-for-loop-to-list-get-the-counter-value"></a>

    4. Use For Loop to List Get the Counter Value

    Using for loop we can iterate the list for every iteration we can get each value of a list. If we want to get the counter value along with the corresponding value of the list we can initialize the counter. For every iteration, the counter will be increased by '1' and return the counter with its corresponding value. For example,

    
    # Manually get the count using for loop 
    courses = ['java','python','pandas','sparks']
    count = 0
    for value in courses:
        count = count + 1
        print(count, value)
    
    # Output:
    # 1 java
    # 2 python
    # 3 pandas
    # 4 sparks
    

    The above counter values in a for loop have been increased manually. Using Python enumerate() function we can get counter values in a for loop automatically.

    <a id="h-5-use-python-enumerate-with-for-loop-get-the-counter-value"></a>

    5. Use Python enumerate() with For Loop Get the Counter Value

    When we use enumerate() with for loop, it returns each value of an iterable object with a counter. For each iteration, the count value is increased by '1'. Here, I will take the list as an iterable object and pass it into enumerate() function then iterate it using for loop. It will traverse each value in a given object and return each value with the corresponding counter. By default count values start from '0'.

    
    # Using enumerate() in a for loop & get the counter
    courses=['java','python','pandas','sparks']
    for value in enumerate(courses):
        print(value)
    

    Yields below output.

    5.1 Start loop counting with non-zero value

    We can customize the starting point of the iterable object count using enumerate() function. For that, we need to set the 'start' parameter with the specified number and pass it into this function and iterate using for loop. It will return each value of iterable object with specified starting point of count value.

    
    # Using enumerate() to set the starting count
    courses=['java','python','pandas','sparks']
    for value in enumerate(courses, start = 1):
        print(value)
    

    Yields below output.

    <a id="h-6-conclusion"></a>

    6. Conclusion

    In this article, I have explained Python enumerate() function and using this syntax and parameters how we can count the values in a for loop with examples. And also explained how we can get the counter values in a for loop with and without enumerate() function.

    Related Articles