Club.noww.in
Wondering what enumerate does in Python? It wraps any iterable and hands you each item together with its position, as an (index, item) pair. So in a for loop you get the counter for free:
for i, park in enumerate(["Yellowstone", "Zion"]): print(i, park) # 0 Yellowstone, then 1 ZionAll the output below is copied from Python 3.12.5 on Windows, and the screenshots show the same runs in Command Prompt.
I’ll show the basic loop, the start argument, why it beats range(len()), using it on strings, dicts and files, and a few tricks with zip().
How enumerate works in a Python for loop
enumerate() produces tuples, and the for i, park in ... line unpacks each one into two variables. Wrapping it in list() shows exactly what’s coming out:
parks = ["Yellowstone", "Yosemite", "Zion"] for i, park in enumerate(parks): print(i, park) print(list(enumerate(parks))) # what enumerate actually produces print(enumerate(parks)) # a lazy enumerate objectOutput:
0 Yellowstone 1 Yosemite 2 Zion [(0, 'Yellowstone'), (1, 'Yosemite'), (2, 'Zion')] <enumerate object at 0x00000153FC825E90>
Counting starts at 0. list(enumerate(parks)) reveals the (index, item) tuples behind the loop.Printing the object itself just shows <enumerate object at ...>. It’s lazy, so it makes each pair only when the loop asks for it.
Starting enumerate at 1 (or any number)
Humans count from 1, so numbered lists usually look wrong starting at 0. Pass start=1, or any other integer, as the second argument:
tasks = ["File taxes", "Renew license", "Book flights"] for n, task in enumerate(tasks, start=1): print(f"{n}. {task}") for n, task in enumerate(tasks, 101): # start can be any integer print(f"Ticket #{n}: {task}")Output:
1. File taxes 2. Renew license 3. Book flights Ticket #101: File taxes Ticket #102: Renew license Ticket #103: Book flights
start only changes the counter. The items still come from the beginning of the list.Why use enumerate instead of range(len())?
You’ll often see for i in range(len(items)) in older code. It works, but you then need items[i] everywhere. enumerate() gives you the value directly:
prices = [19.99, 5.49, 102.00] # the old way for i in range(len(prices)): print(i, prices[i]) # the Pythonic way for i, price in enumerate(prices): print(i, price) # changing items in place still needs the index for i, price in enumerate(prices): prices[i] = round(price * 1.08, 2) # add 8% tax print(prices)Output:
0 19.99 1 5.49 2 102.0 0 19.99 1 5.49 2 102.0 [21.59, 5.93, 110.16]You still use the index when you change items in place, as in the tax example. For more patterns, see Python for loop with index.
Using enumerate on strings, dictionaries and files
Anything you can loop over works with enumerate(). With a dict, enumerate .items() and unpack the pair in parentheses. With a file, you get line numbers:
for i, letter in enumerate("Ohio"): print(i, letter, end=" ") print() capitals = {"TX": "Austin", "CA": "Sacramento"} for i, (state, city) in enumerate(capitals.items(), start=1): print(i, state, city) with open("orders.txt", "w") as f: f.write("order 1001\norder 1002\n\norder 1004\n") with open("orders.txt") as f: for line_no, line in enumerate(f, start=1): if not line.strip(): print("blank line at", line_no)Output:
0 O 1 h 2 i 3 o 1 TX Austin 2 CA Sacramento blank line at 3
The file example reports the blank line as line 3, because start=1 matches how editors number lines.That line-number trick is my go-to for reporting errors in CSV or log files. Python dictionary index has more on positions in a dict.
enumerate with zip, comprehensions and next()
Combine it with zip() to number two lists at once, or use it in a comprehension to find every index of a value. Remember it’s an iterator, so it only runs once:
scores = [88, 92, 75, 92, 61] print([i for i, s in enumerate(scores) if s == 92]) # every index of 92 print({name: i for i, name in enumerate(["Ann", "Bo", "Cy"])}) # name -> position cities = ["Reno", "Mesa", "Waco"] states = ["NV", "AZ", "TX"] for i, (city, state) in enumerate(zip(cities, states), start=1): print(i, city, state) numbered = enumerate(scores) print(next(numbered), next(numbered)) # it's an iterator print(len(list(numbered)), "left") # the first two are already used print(len(list(numbered)), "on a second pass")Output:
[1, 3] {'Ann': 0, 'Bo': 1, 'Cy': 2} 1 Reno NV 2 Mesa AZ 3 Waco TX (0, 88) (1, 92) 3 left 0 on a second pass
After two next() calls only three pairs are left, and a second pass gets nothing at all.If you need to loop twice, call enumerate() again or save list(enumerate(...)) once. The Python zip() function guide covers the zip side.
enumerate() in Python: quick reference
| enumerate(items) | (0, first), (1, second), … |
| enumerate(items, start=1) | (1, first), (2, second), … |
| list(enumerate(items)) | A list of (index, item) tuples |
| enumerate(d.items()) | (0, (key, value)), … |
| enumerate(zip(a, b)) | (0, (a0, b0)), … |
A few related loop guides worth a look:
- Python for loop
- Break and continue in Python
- len() function in Python
- What does in mean in Python
- List of lists in Python
Frequently asked questions
What does enumerate do in Python?
It pairs each item of an iterable with a counter, returning (index, item) tuples you can unpack in a for loop.
How do I make enumerate start at 1?
Pass a start value: enumerate(items, start=1) or just enumerate(items, 1).
What does enumerate return?
An enumerate object, which is an iterator. Wrap it in list() to see all the pairs at once.
Can I use enumerate with a dictionary?
Yes. enumerate(d) numbers the keys, and enumerate(d.items()) numbers the key-value pairs.
Is enumerate faster than range(len())?
They’re about the same speed. People prefer enumerate() because it’s clearer and works on any iterable, not just lists.
Can enumerate count backwards?
Not directly. Use reversed(list(enumerate(items))) to walk it backwards. The official enumerate() docs show its exact definition.












English (US)