Python enumerate() Function: Syntax and Examples

By Dr. Zubair Khalid, DVM, MS, PhD ·

Python enumerate() Function: Syntax and Examples

When you loop over a sequence and need both the item and its position, enumerate() gives you both at once. It returns pairs of (index, value) as you iterate, so you never have to maintain a counter variable by hand. This article covers the syntax, the start argument, a worked example, and the mistakes that trip people up when they use enumerate in Python.

Quick Answer

  • enumerate(iterable, start=0) yields (index, value) tuples, one per item [1].
  • The default index starts at 0. Pass start=1 to number items from 1.
  • It works on any iterable: lists, tuples, strings, dictionaries, generators, files.
  • The index is a plain integer counter, not a lookup key. It does not change the original data.
  • You can unpack the pair directly in the loop header: for i, value in enumerate(items): [1].

What Is enumerate() in Python?

enumerate() is a built-in Python function that pairs each item in a sequence with a running integer count, returning (index, value) tuples one at a time as you loop. It lets you get both the position and the value in a single for loop without writing a separate counter variable. The default count starts at 0, and you can change it with the start argument.

The basic call looks like enumerate(iterable, start=0). The first argument is anything you can loop over: a list, tuple, string, dictionary, generator, or file. The optional second argument is the integer the counter begins at. The function hands back an enumerate object, which is an iterator, so each step produces one two-element tuple: the current count, then the next item. You almost always unpack that tuple right in the loop header, as in for i, value in enumerate(items):.

A quick example makes it concrete. Given items = ["a", "b", "c"], the loop for i, v in enumerate(items): print(i, v) prints 0 a, then 1 b, then 2 c. If you write enumerate(items, start=1) instead, the same three items are labeled 1 a, 2 b, 3 c. Only the numbering shifts; every item is still visited.

What you wantHow to write it
Position and value togetherfor i, v in enumerate(seq):
Numbering that starts at 1enumerate(seq, start=1)
A reusable list of pairslist(enumerate(seq))
Position plus key from a dictfor i, key in enumerate(d):
Position, key, and valuefor i, (k, v) in enumerate(d.items()):

The function does not sort, filter, or change your data. It only attaches positions in the order the iterable produces items. The count is a plain integer, not a lookup key, so it is not the same as an ID column in your data. Because the result is an iterator, it is used up after one pass; if you need the pairs again, wrap the call in list() to store them. The name is also easy to confuse with Python's enum module, which defines named constants and has nothing to do with numbering loops [2].

Syntax

enumerate(iterable, start=0)

ArgumentRequired?Meaning
iterableYesAny object you can iterate over, such as a list, tuple, string, dict, or generator.
startNoThe integer the counter begins at. Defaults to 0.

The function returns an enumerate object, which is an iterator. Each step of that iterator produces a two-element tuple: the current count, then the item from the iterable [1].

How It Works

enumerate() wraps your iterable and keeps an internal counter. On each iteration it hands back a tuple of the counter value and the next item. The counter increments by one after every item.

Because the result is an iterator, it is consumed once. If you loop over the same enumerate object twice, the second loop produces nothing. Wrap it in list() if you need to reuse the pairs.

The start argument only shifts the numbering. It does not skip items or change which items you see. enumerate(items, start=1) still visits every item, just labeled 1, 2, 3 instead of 0, 1, 2.

For dictionaries, iterating gives you keys only. If you want keys with positions, enumerate() works fine. If you want key and value together, the items() method is the right tool [1].

Worked Example

The dataset is a list of four survey response labels for a 4-item Likert question. Each label gets paired with its position.

indexlabel
0Strongly agree
1Agree
2Neutral
3Disagree

Step by step, using the computed values:

  1. The input list has len(labels) = 4.
  2. Iteration 0 yields index=0, label='Strongly agree'.
  3. Iteration 1 yields index=1, label='Agree'.
  4. Iteration 2 yields index=2, label='Neutral'.
  5. Iteration 3 yields index=3, label='Disagree'.
  6. Building a pandas Series with a custom index gives pd.Series(labels, index=[0, 1, 2, 3]), whose index is [0, 1, 2, 3] and whose values are the four labels.
labels = ["Strongly agree", "Agree", "Neutral", "Disagree"]

for i, label in enumerate(labels):
    print(i, label)

import pandas as pd
series = pd.Series(labels, index=[i for i, _ in enumerate(labels)])
print(series)
Output:
0 Strongly agree
1 Agree
2 Neutral
3 Disagree

0    Strongly agree
1             Agree
2           Neutral
3          Disagree
dtype: object

The loop prints the same index-value pairs that become the Series index and values. That is the practical value of enumerate in Python: the position you generate in a loop matches the label you attach to a data structure.

More Examples

Numbering from 1. Reports usually start at 1, not 0.

for rank, label in enumerate(labels, start=1):
    print(rank, label)  # 1 Strongly agree, 2 Agree, ...

Finding the position of a match. Combine enumerate with a condition to get the index of the first item that meets it.

for i, label in enumerate(labels):
    if label == "Neutral":
        print(i)  # 2
        break

Building a lookup dictionary. Turn a list into a position-to-value map.

position_map = dict(enumerate(labels))
print(position_map[2])  # Neutral

Looping over two lists together. When you need paired items from two sequences, zip() is the tool, and it pairs entries by position [1]. You can combine it with enumerate if you also want a counter.

questions = ["name", "quest", "favorite color"]
answers = ["lancelot", "the holy grail", "blue"]
for i, (q, a) in enumerate(zip(questions, answers)):
    print(i, q, a)

If you are still getting comfortable with loop headers, the Python for loop guide covers the patterns that pair well with enumerate. When you want to transform every item instead of numbering it, see the Python map() function.

Errors and How to Fix Them

TypeError: 'int' object is not iterable. You passed a single number, like enumerate(5). Pass a sequence or range instead, such as enumerate(range(5)).

TypeError: 'str' object cannot be interpreted as an integer. You passed a non-integer start, such as enumerate(items, start="1"). Pass an integer: enumerate(items, start=1).

ValueError: not enough values to unpack. You wrote for i, v in enumerate(...) but the loop body expects a different shape, or you tried to unpack a single value into two names. Match the number of names to the tuple size.

TypeError: 'enumerate' object is not subscriptable. You tried enumerate(items)[0]. The result is an iterator, not a list. Convert it first with list(enumerate(items)).

Empty output on a second loop. You reused an exhausted enumerate object. Create a fresh one, or materialize the pairs into a list.

Common Mistakes

  • Using enumerate when you only need the values. If you never use the index, a plain for item in items: loop is clearer. The fix is to drop enumerate.
  • Forgetting that the default start is 0. Off-by-one errors in reports come from assuming 1-based numbering. Pass start=1 when the output is for people.
  • Mutating the list inside the loop. Adding or removing items while iterating changes what enumerate sees. Build a new list instead.
  • Confusing enumerate with the enum module. They share a name root but do different jobs. enum creates named constants, while enumerate numbers items in a loop [2].
  • Assuming enumerate sorts or filters. It does neither. It only attaches positions in the order the iterable produces items.
  • Reusing the enumerate object. Iterators are single-use. Rebuild it or store the pairs.

Limitations

enumerate() numbers items in the order the iterable yields them. It has no concept of a meaningful key. If your data has an ID column, the position from enumerate is not that ID, and treating it as one causes join errors downstream.

It also cannot reorder, deduplicate, or group items. For grouping by a column value you need a dictionary or a dataframe groupby. For sorting you need sorted(). And because the counter is positional, it changes whenever the underlying order changes, so do not store enumerate indices as permanent identifiers.

How enumerate() Compares to Other Looping Tools

Choosing between enumerate(), range(), zip(), and a manual counter comes down to what information your loop needs. Each tool solves a different problem, and mixing them up is a common source of bugs.

enumerate() versus range(len())

A frequent beginner pattern is for i in range(len(items)): value = items[i]. This works, but it is longer and does two lookups: one to get the length, one to fetch each item by index. enumerate() does the same job in one step and reads more naturally: for i, value in enumerate(items):. The range(len()) form is only worth keeping when you genuinely need to index into a second, parallel structure by position, and even then zip() is usually clearer.

enumerate() versus zip()

zip() pairs items from two or more sequences by position. enumerate() pairs items from one sequence with a counter. They answer different questions. If you have questions and answers in two lists and want them side by side, zip() is the tool [1]. If you also want a running number for each pair, nest them: for i, (q, a) in enumerate(zip(questions, answers)):. The outer enumerate supplies the count, the inner zip supplies the paired values, and the double parentheses unpack both layers.

enumerate() versus a manual counter

Writing i = 0 before a loop and i += 1 inside it is the old way. It works, but it adds a line of state you must remember to update, and forgetting the increment or placing it in the wrong branch causes off-by-one errors. enumerate() keeps that counter internal, so there is nothing to forget. The loop body stays focused on the data.

When a plain loop is better

If you never use the index, drop enumerate(). A plain for item in items: is shorter and signals to readers that position does not matter. Reaching for enumerate out of habit when the count is unused adds noise.

Worked Examples Across Common Data Types

The function behaves the same way on every iterable, but the shape of what you get back changes with the data type. These examples show the differences.

Lists and tuples

For a list or tuple, each pair is (position, element). Given colors = ("red", "green", "blue"), the loop for i, c in enumerate(colors): print(i, c) prints 0 red, 1 green, 2 blue. Converting the result with list(enumerate(colors)) gives [(0, 'red'), (1, 'green'), (2, 'blue')], which you can store, slice, or pass to another function.

Strings

A string is an iterable of characters, so enumerate gives you each character with its position. For word = "cat", list(enumerate(word)) is [(0, 'c'), (1, 'a'), (2, 't')]. This is handy for tasks like finding the position of a letter, though str.find() is the direct tool when you only need one match.

Dictionaries

A plain loop over a dictionary yields keys only, so enumerate(my_dict) gives positions plus keys, not values. To get the value too, loop over my_dict.items() and unpack both layers: for i, (key, value) in enumerate(my_dict.items()):. Here i is the position, key is the dictionary key, and value is what it maps to. Forgetting the inner parentheses is a common mistake and raises an unpacking error.

Generators and files

Because enumerate accepts any iterable, it works on generators and open files without loading everything into memory. Reading a file line by line with for line_no, line in enumerate(open("data.txt"), start=1): numbers the lines as they stream in. The start=1 matches how people count lines in an editor. The same streaming behavior applies to generators, so enumerate stays memory-friendly on large inputs.

A diagram in words

Picture the enumerate object as a conveyor belt. Your iterable feeds items onto the belt one at a time. A small counter sits beside the belt, starting at the value you gave for start. As each item passes, the counter stamps it with its current number and hands you the stamped pair, then ticks up by one. The belt moves forward only; once an item has passed, it does not come back. That is why a second loop over the same enumerate object finds an empty belt.

Misconceptions and Edge Cases

Several beliefs about enumerate are widespread and wrong. Clearing them up prevents real bugs.

"enumerate changes my list"

It does not. The function reads from your iterable and produces new tuples; the original list, tuple, or dictionary is untouched. If you want the numbered pairs as data, build a new list with list(enumerate(items)). The source stays as it was.

"The index is a stable ID"

The count reflects position in the current order. Sort the list, filter it, or insert an item, and every later position shifts. Storing enumerate indices as permanent identifiers in a database or a join key leads to mismatches when the order changes. Use a real ID column for that purpose.

"start skips items"

start only changes the label on the first item. enumerate(items, start=5) still visits every item, numbering them 5, 6, 7, and so on. It does not drop the first five items. To skip items, slice the iterable first, as in enumerate(items[5:]).

"I can index into the result"

The enumerate object is an iterator, not a list, so enumerate(items)[0] raises a TypeError. Convert it first: list(enumerate(items))[0]. The same rule explains why looping twice over one enumerate object gives nothing the second time; the iterator is exhausted.

"enumerate and enum are related"

They share letters, not purpose. enumerate() numbers items during a loop. The enum module creates named constants such as Color.RED [2]. One is a built-in function for iteration, the other a library for defining fixed sets of names.

Negative and large start values

start accepts any integer, including negatives. enumerate(items, start=-2) labels the first item -2, then -1, 0, and onward. This is rarely needed but behaves exactly as described: the counter begins where you say and increments by one each step.

Practical Patterns That Use enumerate()

Beyond simple numbering, enumerate fits into several everyday tasks. These patterns show where it earns its place.

Building a position-to-value dictionary

dict(enumerate(items)) turns a list into a map from position to value in one line. For ["x", "y", "z"] you get {0: 'x', 1: 'y', 2: 'z'}. This is useful when later code needs to look up an item by its slot.

Finding the first match

To get the position of the first item meeting a condition, loop with enumerate and break when it matches: for i, v in enumerate(items): if v == target: print(i); break. This stops at the first hit and avoids scanning the rest. If no item matches, the loop ends without printing, so add a flag or an else clause on the loop if you need to handle that case.

Numbering report lines

When producing human-facing output, start at 1 so the numbering matches how people count. for n, row in enumerate(rows, start=1): print(f"{n}. {row}") prints a numbered list beginning at 1. This is the single most common real use of the start argument.

Pairing with zip for multi-column output

When you have several parallel lists and want a numbered table, nest enumerate around zip: for i, (name, score) in enumerate(zip(names, scores), start=1):. Each row gets a number plus the paired values from both lists. The unpacking order matters: the outer tuple is (count, pair), and the inner pair unpacks into the two names.

Transforming while numbering

If you need to change each item and keep its position, combine enumerate with a comprehension: [(i, v.upper()) for i, v in enumerate(words)]. The result is a list of numbered, transformed values. This keeps the position attached through the transformation, which is useful when the output must stay aligned with the input order.

Frequently Asked Questions

What does enumerate() do in Python?

It pairs each item from an iterable with a running integer index and returns those pairs one at a time. You use it when a loop needs both the value and its position [1]. The default index starts at 0.

How do I start enumerate at 1?

Pass the start argument: enumerate(items, start=1). The first item is then labeled 1, the second 2, and so on. Every item is still visited, only the numbering shifts.

Is enumerate faster than using a counter variable?

It is generally cleaner and avoids manual increment bugs. Both approaches visit each item once, so the difference is small. The real gain is readability and fewer off-by-one mistakes.

Can I use enumerate on a dictionary?

Yes, but a plain loop over a dictionary yields keys, so enumerate gives you positions plus keys. To get keys and values together, use the items() method instead [1]. You can also combine both if you need a position, a key, and a value.

What is the difference between enumerate and the enum module?

enumerate() is a built-in function that numbers items during iteration. The enum module is a separate library for defining named constants, such as Color.RED [2]. The similar names are a coincidence, and the two are unrelated in use.

References

  1. 5. Data Structures, Python 3.14.8 documentation
  2. enum, Support for enumerations, Python 3.14.8 documentation

Further Reading

Related Articles

Related articles