In Python, the “for” loop is used to iterate over the sequence and repeat the program several times. To see how many times a program executes, we need to count each iteration of a program. Python uses various methods to count values in a “for” loop.
This write-up will provide a detailed guide on how to count in a for loop in Python using the following points:
Let’s start with the traditional for loop approach.
Method 1: Using the Traditional “for” Loop Approach
We can count in a “for” loop by incrementing the value of the variable using the additional operator “+”. In the example given below, the element value of the dictionary is counted using the “for” loop and additional operator:
Code:
dict_value = {'Name': 'Alex', 'Age': 44, 'Sex': 'Male'}
count = 0
for i in dict_value:
count += 1
print('Total Number of Items: ', count)
In the above code:
- The “dict_value” containing the key-value pair is initialized in the program.
- The count value is set to “0”.
- The “for” loop iterates over the dictionary, and for each iteration, the value of the counter is incremented by “1”.
Output:
The above output counts the number of elements in a dictionary variable.
Method 2: Using enumerate() Function
The “enumerate()” function is used to count the value in the “for” loop. Let’s understand by the following numerous examples:
Example 1: Count in for Loop By Starting From an Initial Index
In the example given below, the “enumerate()” function is used along with the “for” loop to count the element value with the index number:
Code:
dict_value = {'Name': 'Alex', 'Age': 44, 'Sex': 'Male'}
for i, item in enumerate(dict_value):
print(i, item)
In the above code:
- The enumerate() function accepts the dictionary variable as an argument and returns the enumerate object.
- The “for” loop iterates over the enumerated object and adds the counter value as the key.
Output:
The index number and key names of the given dictionary are displayed using the “for” loop and enumerate() function.
Example 2: Count in for Loop By Starting With Specific Index
We can start the count by specifying the specific index value in the “enumerate()” function. In the example given below, the “enumerate()” function is used to count the dictionary variable:
Code:
dict_value = {'Name': 'Alex', 'Age': 44, 'Sex': 'Male'}
for i, item in enumerate(dict_value, start=5):
print(i, item)
The “enumerate()” function accepts the dictionary variable as a first argument and the “5” value is assigned to the start parameter which indicates that the index number starts from “5”.
Output:
The dictionary items starting from index number “5” have been displayed on the screen.
That’s how users can count in for a loop in Python.
Conclusion
To count in a “for” loop, the traditional “for” loop, along with the addition operator “+” and the “enumerate()” function, is used in Python. The “enumerate()” function can also be used to start the count in a “for” loop by a specific index number. This article has presented various methods to count in a “for” loop in Python.