The enumerate function in Python generates an enumerated object. It requires two arguments: the first is a sequence of items, while the second specifies the starting index of the sequence. We can access the elements of the sequence using either a loop or the next method.
The next function of the iterator produced by enumerate yields a tuple containing the values extracted from iterating through a sequence.
Python Enumerate Function Syntax
Here is the syntax for the enumerate function:
enumerate (sequence, start=0)
Parameters
- sequence : It must be a collection or a sequence. It is the required parameter.
- start : It is an optional index, used to set starting of the sequence.
Return
This approach yields an instance of the enumerated type.
Different Examples for Python enumerate Function
Let’s explore a few illustrations of the enumerate function to grasp its capabilities.
Example 1:
This serves as an example of how to employ the enumerate function:
# create a list of fruits
fruits = ['apple', 'banana', 'cherry']
# iterate over the list using the enumerate() function
# the enumerate() function returns a tuple containing the index and value of each element
for i, fruit in enumerate(fruits):
# print the index and value of each element
print(i, fruit)
Output:
0 apple
1 banana
2 cherry
Explanation:
In this example, the enumerate function is employed to iterate over the fruits list, and for each item in the list, a tuple is generated that includes both the index and the associated value. Subsequently, the for loop outputs each tuple, resulting in the output shown above.
Example 2:
A common scenario for utilizing the enumerate function arises when there is a need to iterate over a sequence while simultaneously accessing the index of each element. For example, consider a list of numbers where you wish to identify the index of the first negative number. The enumerate function can be employed to accomplish this in the following manner:
# create a list of numbers
numbers = [1, 2, -3, 4, -5, 6]
# iterate over the list using the enumerate() function
# the enumerate() function returns a tuple containing the index and value of each element
for i, num in enumerate(numbers):
# check if the value is negative
if num < 0:
# print a message containing the index and value of the first negative number
print(f"The first negative number is {num} at index {i}.")
# break out of the loop since we found the first negative number
break
Output:
The first negative number is -3 at index 2.
Explanation:
In this illustration, the enumerate function is employed to iterate over the list of numbers, generating a tuple for each element that consists of its index and the corresponding value. The if statement verifies whether the value is negative, and if so, it outputs a message that includes both the index and the value of the first negative number encountered.
Example 3:
A further application of the enumerate function arises when you wish to create a dictionary that associates each item in a sequence with its corresponding index. You can leverage a dictionary comprehension along with the enumerate function to accomplish this in the following manner:
# create a list of fruits
fruits = ['apple', 'banana', 'cherry']
# create a dictionary using a dictionary comprehension and the enumerate() function
# the enumerate() function returns a tuple containing the index and value of each element
# the dictionary comprehension maps each fruit name to its index in the list
fruit_dict = {fruit: i for i, fruit in enumerate(fruits)}
# print the resulting dictionary
print(fruit_dict)
Output:
{'apple': 0, 'banana': 1, 'cherry': 2}
Explanation:
In this example, the enumerate function is employed to iterate through the fruits list, and for each element in the list, a tuple is generated that consists of the index and the corresponding value. The dictionary comprehension constructs a dictionary where each key represents a fruit name and each value corresponds to its respective index.
Conclusion
To summarize, the enumerate function in Python serves as a valuable tool for iterating through a sequence while keeping track of the index of each item. It can be applied in a multitude of scenarios, ranging from straightforward loops to more intricate operations such as constructing dictionaries. This functionality is particularly advantageous when you require access to both the index and the corresponding value of each element during iteration.