How to Use Enumerate in Python (Looping with Indices)
Learn how to use the enumerate() function in Python. Get both index and value when looping over lists with clean examples.
Explanation
When iterating over a list, tuple, or string, you will often need to track the current index of each item alongside the item itself. A common beginner pattern is using a separate counter variable that is incremented manually, or using `range(len(my_list))` to iterate via indices. However, Python provides a far more elegant, pythonic, and built-in solution: the `enumerate()` function.
The `enumerate()` function takes any iterable as an argument and returns an enumerate object, which yields pairs of index-value tuples. By using tuple unpacking in a `for` loop, you can extract the index and the item simultaneously. This completely removes the need for manual counter variables and indexing brackets, making your code significantly cleaner and less prone to off-by-one errors.
By default, `enumerate()` starts counting indices from `0`. However, you can pass a custom starting index using the optional `start` keyword argument (e.g., `enumerate(items, start=1)`). This is incredibly useful when displaying list numbers to human users, as people typically expect lists to start counting from 1 instead of 0.
Step-by-Step Implementation
- 1
Pass your list (or any iterable) to the built-in enumerate() function.
- 2
Unpack the returned tuples in a for loop using the syntax for index, value in enumerate(list).
- 3
Provide the start parameter (e.g., start=1) to modify the initial index counter value.
Code Example
This script demonstrates iterating over lists using enumerate to get index-item pairs, starting from 0 and 1.
fruits = ["apple", "banana", "cherry"]
# 1. Standard enumeration (starts at 0)
print("--- Standard Enumeration ---")
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 2. Enumeration starting at a custom index (e.g., 1)
print("\n--- Starting at 1 ---")
for num, fruit in enumerate(fruits, start=1):
print(f"Item #{num}: {fruit}")--- Standard Enumeration ---
Index 0: apple
Index 1: banana
Index 2: cherry
--- Starting at 1 ---
Item #1: apple
Item #2: banana
Item #3: cherryFrequently Asked Questions
Does enumerate() modify the original list?
No, enumerate() returns a lazy iterator object and leaves the original list entirely unmodified.
Can I use enumerate() with a dictionary?
Yes, but iterating over a dictionary directly will yield its keys. To iterate index-key-value, use: for i, (k, v) in enumerate(my_dict.items()):.
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.