🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

enumerate()

AI & DATA SCIENCE // enumerate

enumerate() wraps an iterable, producing (index, item) pairs so you can loop with both a counter and the value at once.

Syntax

enumerate(iterable, start=0)

Deep Dive Course

Without enumerate(), getting both an index and a value inside a for loop means manually tracking a counter or indexing with range and len. enumerate() instead returns a lazy iterator of index-item tuples starting at 0, or at whatever number you pass as start, and is almost always unpacked directly in the loop header.

1Understanding enumerate()

Without enumerate(), getting both an index and a value inside a for loop means manually tracking a counter or indexing with range and len. enumerate() instead returns a lazy iterator of index-item tuples starting at 0, or at whatever number you pass as start, and is almost always unpacked directly in the loop header.

💡

Pass start=1 to enumerate() instead of the default of 0 when you want human-friendly, 1-based numbering, for example when displaying a numbered list in a UI or CLI output.

editor.html
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
    print(i, fruit)
localhost:3000

2Practical Example

Here is a real-world application of enumerate() showing how it is used in production Python code.

editor.html
tasks = ["Design", "Build", "Ship"]
for step_num, task in enumerate(tasks, start=1):
    print(f"Step {step_num}: {task}")
localhost:3000

3Best Practices

Follow these guidelines when working with enumerate():

1. Use enumerate(items) instead of range(len(items)) whenever you need both the index and the value

2. Pass start=1 for 1-based counting instead of adding 1 to the index manually inside the loop

3. Unpack the tuple directly in the for statement rather than indexing into it afterward

⚠️

Tip: Pass start=1 to enumerate() instead of the default of 0 when you want human-friendly, 1-based numbering, for example when displaying a numbered list in a UI or CLI output.

editor.html
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
    print(i, fruit)
localhost:3000

Examples

Example 01Basic Usage
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
    print(i, fruit)
Example 02Advanced Example
tasks = ["Design", "Build", "Ship"]
for step_num, task in enumerate(tasks, start=1):
    print(f"Step {step_num}: {task}")

Best Practices

  • Use enumerate(items) instead of range(len(items)) whenever you need both the index and the value
  • Pass start=1 for 1-based counting instead of adding 1 to the index manually inside the loop
  • Unpack the tuple directly in the for statement rather than indexing into it afterward

Interview Question

Why is looping with enumerate() generally preferred over looping over range(len(items))?

Hint: Think about what you have to do to actually get the item in each version.

With range(len(items)), you only get the index and must then index back into the list to get the value, which is an extra lookup and a common source of off-by-one bugs. enumerate() gives you both the index and the item directly as a tuple in one pass, is more readable, and works on any iterable, not just ones that support indexing, like a generator.

Exercises

MediumPractice using enumerate() in a real scenario.
View Solution
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
    print(i, fruit)

Frequently Asked Questions

Why is looping with enumerate() generally preferred over looping over range(len(items))?

With range(len(items)), you only get the index and must then index back into the list to get the value, which is an extra lookup and a common source of off-by-one bugs. enumerate() gives you both the index and the item directly as a tuple in one pass, is more readable, and works on any iterable, not just ones that support indexing, like a generator.

Related Functions

zip()range()for loop