The enumerate()
function in Python is a built-in function that provides an easy way to iterate over a sequence (such as a list, tuple, or string) while keeping track of the index of the current item. It returns an iterator that generates tuples of the form (index, element)
.
The syntax for enumerate()
is as follows:
enumerate(iterable, start=0)
Code language: Python (python)
iterable
: This is the sequence (list, tuple, string, etc.) that you want to iterate over.start
: (Optional) This parameter specifies the starting value of the index. The default value is 0, but you can specify a different integer value if you want the index to start from a different number.
Here’s an example of how to use enumerate()
:
fruits = ['apple', 'banana', 'orange', 'grape']
for index, fruit in enumerate(fruits):
print(f"Index: {index}, Fruit: {fruit}")
Code language: Python (python)
Output:
Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: orange
Index: 3, Fruit: grape
Code language: Python (python)
In this example, the enumerate()
function is used in the for loop to iterate over the fruits
list. In each iteration, the index
variable holds the current index, and the fruit
variable holds the value of the current fruit from the list.
enumerate()
is particularly useful when you need to iterate over a sequence and also need to know the corresponding index of each item. It saves you from manually managing a separate index variable and makes your code more concise and readable.
How do you enumerate a list in Python for loop?
In Python, you can use the built-in enumerate()
function to iterate through a list and get both the index and the corresponding element in each iteration. The enumerate()
function returns a tuple of (index, element).
Here’s the basic syntax for using enumerate()
in a for loop:
my_list = [10, 20, 30, 40, 50]
for index, element in enumerate(my_list):
print(f"Index: {index}, Element: {element}")
Code language: Python (python)
In this example, my_list
contains five elements, and the enumerate()
function is used in the for loop to iterate over each element in the list. In each iteration, the index
variable will hold the index of the current element, and the element
variable will hold the value of the current element.
The output will be:
Index: 0, Element: 10
Index: 1, Element: 20
Index: 2, Element: 30
Index: 3, Element: 40
Index: 4, Element: 50
Code language: Python (python)
As you can see, the loop has enumerated the list and printed both the index and the corresponding element for each iteration. This can be useful when you need to keep track of the index while iterating over the elements of the list.
Read More;
- How to use for loop in JSON Python?
- What is lambda function in Python for example?
- What is multilevel inheritance in Python with example?
- How do you use a any () or a all () in Python?
- How to add Qt to Python With Example
- What are type hints in Python With Example?
- Python Script Example for DevOps
- What is RegEx in Python with example?
- What is an example of a Boolean in Python?
- What is an example of built-in and user-defined functions in Python?
- What is an example of a constructor in Python?
- What is a dictionary in Python example?