Get Counter Values in a For Loop in Python?

Spread the love

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 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 how to count each iteration of a given iterable object in a for loop using enumerate() and range() functions.

1. Quick Examples of Counter Values in a For Loop in Python

Following are quick examples of count iteration in a for loop in Python.


#Below are the quick examples

# Example 1: Using enumerate() to get the count in a for loop
courses=['java','python','pandas','sparks']
for index , value in enumerate(courses):

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

# Example 3: Using range() to count the iteration
courses = ['java','python','pandas','sparks']
for index in range(len(courses)):
    value = courses[index]

# Example 4: Initialize the count value in a for loop
courses=['java','python','pandas','sparks']
count = 0
for value in courses:
    count += 1

2. Syntax of enumerate()

Following is a syntax of enumerate() function.


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

2.1 Parameters

It allows two parameters.

  • iterable_object: ( such as list, tuple) It supports iteration.
  • start : Using this we can specify starting point of counter, by default it is ‘0’.

2.2 Return Value

It returns enumerate object.

3. Get the Counter Values in a For Loop using Python enumerate()

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() to get the count in a for loop
courses=['java','python','pandas','sparks']
for index , value in enumerate(courses):
    print(index, value)

Yields below output.

python for loop counter

3.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 index , value in enumerate(courses, start = 1):
    print(index, value)
python for loop counter

4. Get Counter Values in a For Loop Using Python range()

Alternatively, we can use the range() function with for loop to return each value of an iterable object with a corresponding counter. Using the range() function we can represent a sequence of values. so that, we can iterate each value present in the sequence using loops.

Let’s take a list as an iterable object and pass the length of the list into range() function Then, using for loop we will iterate each value in an object and for every iteration, the counter value will be increased by ‘1’. Finally, it will return each value of a given iterable object with its corresponding counter value.


# Using range() to count the iteration
courses = ['java','python','pandas','sparks']
for index in range(len(courses)):
    value = courses[index]
    print(index, value)

Yields below output.


# Output:
0 java
1 python
2 pandas
3 sparks

5. Initialize the Counter Value in Python For Loop

Alternatively, we can initialize the count value in a for loop and get the count of each value in an iterable object. Take 'count' as a variable and initialize it with '0'. For every iteration, ‘count’ variable is added by’1′. Finally, it will return all count values of given object. For example,


# Initialize the count value in a for loop
courses=['java','python','pandas','sparks']
count = 0
for value in courses:
    count += 1
    print(count)

Yields below output.


# Output:
1
2
3
4

6. Conclusion

In this article, I have explained how to count each iteration of a given iterable object in a for loop using enumerate() and range() functions in Python with examples.

Related Articles

References

Leave a Reply

You are currently viewing Get Counter Values in a For Loop in Python?