The enumerate() function is used when you need to loop through an iterable while getting both the index and the value at the same time.
It is commonly used with:
- Lists
- Tuples
- Strings
- Dictionaries
- Other iterable objects
Basic Syntax
enumerate(iterable, start=0)The default starting index is 0.
Example:
names = ["Ram", "Sita", "Hari"]
for index, name in enumerate(names):
print(index, name)Output:
0 Ram
1 Sita
2 Hari1. Why Use enumerate()?
Without enumerate(), you might write:
names = ["Ram", "Sita", "Hari"]
for i in range(len(names)):
print(i, names[i])Output:
0 Ram
1 Sita
2 HariWith enumerate():
names = ["Ram", "Sita", "Hari"]
for i, name in enumerate(names):
print(i, name)It is shorter and easier to read.
2. Basic Example
fruits = ["Apple", "Banana", "Mango"]
for index, fruit in enumerate(fruits):
print(index, fruit)Output:
0 Apple
1 Banana
2 MangoHere:
indexcontains the position, while:
fruitcontains the actual value.
3. Start Index from 1
By default, indexing starts from 0.
You can change this using the start parameter.
names = ["Ram", "Sita", "Hari"]
for index, name in enumerate(names, start=1):
print(index, name)Output:
1 Ram
2 Sita
3 HariThis is very useful when creating numbered lists.
4. Start Index from 100
The starting value doesn't have to be 1.
names = ["Ram", "Sita", "Hari"]
for index, name in enumerate(names, start=100):
print(index, name)Output:
100 Ram
101 Sita
102 HariThe index increases by 1 for each item.
5. enumerate() with Strings
Strings are iterable.
word = "Python"
for index, character in enumerate(word):
print(index, character)Output:
0 P
1 y
2 t
3 h
4 o
5 n6. Start String Index from 1
word = "Python"
for position, character in enumerate(word, start=1):
print(position, character)Output:
1 P
2 y
3 t
4 h
5 o
6 n7. enumerate() with Tuples
languages = ("Python", "JavaScript", "PHP", "Java")
for index, language in enumerate(languages):
print(index, language)Output:
0 Python
1 JavaScript
2 PHP
3 Java8. enumerate() with a Dictionary
When directly iterating over a dictionary, enumerate() gives the index and dictionary key.
students = {
"Ram": 75,
"Sita": 85,
"Hari": 65
}
for index, name in enumerate(students):
print(index, name)Output:
0 Ram
1 Sita
2 HariIf you need both the student name and marks:
students = {
"Ram": 75,
"Sita": 85,
"Hari": 65
}
for index, (name, marks) in enumerate(students.items()):
print(index, name, marks)Output:
0 Ram 75
1 Sita 85
2 Hari 659. enumerate() with start=1
This is common when displaying data to users.
students = ["Ram", "Sita", "Hari", "Gita"]
for number, student in enumerate(students, start=1):
print(number, student)Output:
1 Ram
2 Sita
3 Hari
4 GitaYou can use this to create a numbered menu:
courses = [
"Python",
"Django",
"MERN",
"Data Science"
]
for number, course in enumerate(courses, start=1):
print(f"{number}. {course}")Output:
1. Python
2. Django
3. MERN
4. Data Science10. Get a Specific Index
You can use enumerate() to find the position of a particular value.
names = ["Ram", "Sita", "Hari", "Gita"]
for index, name in enumerate(names):
if name == "Hari":
print("Hari is at index:", index)Output:
Hari is at index: 211. Find Index of Multiple Values
names = ["Ram", "Sita", "Hari", "Sita", "Gita"]
for index, name in enumerate(names):
if name == "Sita":
print("Sita found at:", index)Output:
Sita found at: 1
Sita found at: 312. Modify Items Using enumerate()
Suppose we want to modify values in a list.
numbers = [10, 20, 30, 40]
for index, number in enumerate(numbers):
numbers[index] = number * 2
print(numbers)Output:
[20, 40, 60, 80]Here, index allows us to access and update the original list.
13. Add 10 to Every Value
marks = [50, 60, 70, 80]
for index, mark in enumerate(marks):
marks[index] = mark + 10
print(marks)Output:
[60, 70, 80, 90]14. Practical Student Example
students = ["Ram", "Sita", "Hari", "Gita"]
for roll_number, student in enumerate(students, start=1):
print(f"Roll No: {roll_number}, Name: {student}")Output:
Roll No: 1, Name: Ram
Roll No: 2, Name: Sita
Roll No: 3, Name: Hari
Roll No: 4, Name: Gita15. Practical Marks Example
students = ["Ram", "Sita", "Hari", "Gita"]
marks = [75, 85, 65, 90]
for index, student in enumerate(students):
print(student, marks[index])Output:
Ram 75
Sita 85
Hari 65
Gita 90However, when two lists need to be processed together, zip() is often cleaner:
for student, mark in zip(students, marks):
print(student, mark)16. enumerate() with zip()
You can combine enumerate() and zip().
students = ["Ram", "Sita", "Hari"]
marks = [75, 85, 65]
for index, (student, mark) in enumerate(zip(students, marks), start=1):
print(index, student, mark)Output:
1 Ram 75
2 Sita 85
3 Hari 65This is useful when you need:
- Serial number
- Student name
- Marks
all together.
17. Practical Product Example
products = ["Laptop", "Mouse", "Keyboard", "Monitor"]
for number, product in enumerate(products, start=1):
print(f"{number}. {product}")Output:
1. Laptop
2. Mouse
3. Keyboard
4. Monitor18. Practical Course Example
courses = [
"Python Programming",
"Django",
"MERN Stack",
"Data Science",
"Machine Learning"
]
for number, course in enumerate(courses, start=1):
print(f"{number}. {course}")Output:
1. Python Programming
2. Django
3. MERN Stack
4. Data Science
5. Machine Learning19. Create a Dictionary Using enumerate()
You can use enumerate() to create numbered dictionary keys.
courses = ["Python", "Django", "MERN"]
result = {
number: course
for number, course in enumerate(courses, start=1)
}
print(result)Output:
{1: 'Python', 2: 'Django', 3: 'MERN'}This combines enumerate() with dictionary comprehension.
20. Create a List Using enumerate()
You can also create a list containing indexes and values.
names = ["Ram", "Sita", "Hari"]
result = [
(index, name)
for index, name in enumerate(names)
]
print(result)Output:
[(0, 'Ram'), (1, 'Sita'), (2, 'Hari')]21. Find Even Numbers and Their Index
numbers = [10, 15, 20, 25, 30, 35]
for index, number in enumerate(numbers):
if number % 2 == 0:
print(index, number)Output:
0 10
2 20
4 3022. Find the First Matching Item
names = ["Ram", "Sita", "Hari", "Gita"]
for index, name in enumerate(names):
if name == "Hari":
print("Found at index:", index)
breakOutput:
Found at index: 2The break statement stops the loop after the first match.
23. Practical Shopping Cart Example
cart = [
"Laptop",
"Mouse",
"Keyboard",
"Monitor"
]
for number, item in enumerate(cart, start=1):
print(f"{number}. {item}")Output:
1. Laptop
2. Mouse
3. Keyboard
4. Monitor24. Practical To-Do List
tasks = [
"Learn Python",
"Practice Django",
"Build a project",
"Deploy website"
]
for number, task in enumerate(tasks, start=1):
print(f"{number}. {task}")Output:
1. Learn Python
2. Practice Django
3. Build a project
4. Deploy website25. enumerate() vs range(len())
Using range(len())
names = ["Ram", "Sita", "Hari"]
for i in range(len(names)):
print(i, names[i])Using enumerate()
names = ["Ram", "Sita", "Hari"]
for i, name in enumerate(names):
print(i, name)Both produce:
0 Ram
1 Sita
2 HariBut enumerate() is generally cleaner because you don't have to manually access names[i].
26. Common Mistake
Don't do this:
names = ["Ram", "Sita", "Hari"]
for index in enumerate(names):
print(index)The variable index actually contains a tuple:
(0, 'Ram')
(1, 'Sita')
(2, 'Hari')Instead, unpack the two values:
for index, name in enumerate(names):
print(index, name)27. Understanding What enumerate() Returns
names = ["Ram", "Sita", "Hari"]
result = enumerate(names)
print(result)You will see something similar to:
<enumerate object at 0x...>Like generators, enumerate() returns an iterator object.
You can convert it into a list:
names = ["Ram", "Sita", "Hari"]
result = list(enumerate(names))
print(result)Output:
[(0, 'Ram'), (1, 'Sita'), (2, 'Hari')]28. enumerate() with a Generator
enumerate() can work with any iterable, including generators.
numbers = (
x ** 2
for x in range(1, 6)
)
for index, number in enumerate(numbers, start=1):
print(index, number)Output:
1 1
2 4
3 9
4 16
5 25This is a good example of combining the previous topic with the current one.
29. Practical Data Processing Example
employees = [
{"name": "Ram", "salary": 35000},
{"name": "Sita", "salary": 55000},
{"name": "Hari", "salary": 45000}
]
for number, employee in enumerate(employees, start=1):
print(
number,
employee["name"],
employee["salary"]
)Output:
1 Ram 35000
2 Sita 55000
3 Hari 4500030. Real-World Example : Display Course List
courses = [
"Python Programming",
"Python Django",
"MERN Stack",
"Data Analysis",
"Data Science",
"Machine Learning"
]
print("Available Courses:")
for number, course in enumerate(courses, start=1):
print(f"{number}. {course}")Output:
Available Courses:
1. Python Programming
2. Python Django
3. MERN Stack
4. Data Analysis
5. Data Science
6. Machine LearningImportant Points to Remember
Basic syntax
enumerate(iterable)With starting position
enumerate(iterable, start=1)Common usage
for index, value in enumerate(data):
print(index, value)Start from 1
for number, value in enumerate(data, start=1):
print(number, value)enumerate() returns
An enumerate iterator containing pairs of:
(index, value)Main advantage
Instead of:
for i in range(len(items)):
print(i, items[i])you can write:
for i, item in enumerate(items):
print(i, item)Easy way to remember
enumerate()
↓
index + value
↓
(index, value)