0
0
Pythonprogramming~5 mins

enumerate() function in Python

Choose your learning style9 modes available
Introduction

The enumerate() function helps you get both the position (index) and the value from a list or other collection while looping. It makes counting items easy and clear.

When you want to loop over a list and need to know the position of each item.
When you want to print items with their numbers, like a numbered list.
When you want to update items in a list by their position.
When you want to compare items with their index in a loop.
Syntax
Python
enumerate(iterable, start=0)

iterable is any collection you want to loop over, like a list or string.

start is optional and sets the first index number (default is 0).

Examples
This loops over the list and prints each item's position and value, starting counting from 0.
Python
for index, value in enumerate(['apple', 'banana', 'cherry']):
    print(index, value)
This loops over the string 'hello' and starts counting from 1 instead of 0.
Python
for i, letter in enumerate('hello', start=1):
    print(i, letter)
Sample Program

This program prints a numbered list of fruits starting from 1.

Python
fruits = ['apple', 'banana', 'cherry']

for index, fruit in enumerate(fruits, start=1):
    print(f"{index}. {fruit}")
OutputSuccess
Important Notes

You can use enumerate() with any iterable, not just lists.

Using start lets you control the counting number, which is helpful for user-friendly numbering.

Summary

enumerate() gives you both the index and value when looping.

It helps make loops simpler and clearer when you need item positions.

You can choose where counting starts with the start parameter.