Python Reference Library →

Python enumerate() Function – Loop with Index Easily

Let’s look at enumerate() function – loop with index easily

  1. Notice

    enumerate() yields index/value pairs. The optional start changes the first index, not the items.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
    print(index, item)
Live state

Top-level scope

items→Object 1
index→Object 4
item→Object 5
Object 1 · list['tea', 'milk']Object 4 · int4Object 5 · str'milk'
This step

enumerate() yields index/value pairs. The optional start changes the first index, not the items.

Picture enumerate() function – loop with index easily

enumerate() yields index/value pairs. The optional start changes the first index, not the items.

Image brief

Alt text
A diagram for Python enumerate() Function – Loop with Index Easily. Instructions: items = ["tea", "milk"] for index, item in enumerate(items, start=3): print(index, item). Displayed result: 3 tea 4 milk. The final example bindings are items --> object 1 (list); index --> object 4 (int); item --> object 5 (str). object 1: ['tea', 'milk']; object 4: 4; object 5: 'milk'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python enumerate() Function – Loop with Index Easily". Teaching focus: enumerate() yields index/value pairs. The optional start changes the first index, not the items. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = ["tea", "milk"] for index, item in enumerate(items, start=3): print(index, item) | v DISPLAYED RESULT 3 tea 4 milk FINAL BINDINGS items --> object 1 (list) index --> object 4 (int) item --> object 5 (str) object 1: ['tea', 'milk'] object 4: 4 object 5: 'milk' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python enumerate() Function – Loop with Index Easily. Instructions: items = ["tea", "milk"] for index, item in enumerate(items, start=3): print(index, item). Displayed result: 3 tea 4 milk. The final example bindings are items --> object 1 (list); index --> object 4 (int); item --> object 5 (str). object 1: ['tea', 'milk']; object 4: 4; object 5: 'milk'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
    print(index, item)
Live state

Top-level scope

items→Object 1
index→Object 4
item→Object 5
Object 1 · list['tea', 'milk']Object 4 · int4Object 5 · str'milk'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3 tea
4 milk

What do you expect this program to print?

What does this example display?

items = ["tea", "milk"]
for index, item in enumerate(items, start=3):
    print(index, item)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3 tea 4 milk

Follow the printed values in order. The output is: 3 tea 4 milk

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

enumerate() yields index/value pairs. The optional start changes the first index, not the items.

enumerate() yields index/value pairs. The optional start changes the first index, not the items.

More examples and reference

Python enumerate() Function

The enumerate() function in Python is used to loop through a sequence (like a list, tuple, or string) while keeping track of the index of the current item.

It's a clean and Pythonic way to write loops when you need both index and value.

Syntax

enumerate(iterable, start=0)

Parameters:

  • iterable – A sequence like a list, tuple, or string
  • start – The index to start counting from (default is 0)

Returns:

  • An enumerate object which can be used in a loop to get (index, value) pairs

Basic Example

fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print(index, fruit)
0 apple
1 banana
2 cherry

Example: Start Counting from 1

for index, fruit in enumerate(fruits, start=1):
    print(index, fruit)
1 apple
2 banana
3 cherry

Use Case: Replacing Manual Counter in Loops

Instead of writing:

i = 0
for item in fruits:
    print(i, item)
    i += 1

You can simplify it using enumerate():

for i, item in enumerate(fruits):
    print(i, item)

Use Case: Working with Strings

for i, char in enumerate("hello"):
    print(f"Index {i} has character '{char}'")
Index 0 has character 'h'
Index 1 has character 'e'
Index 2 has character 'l'
Index 3 has character 'l'
Index 4 has character 'o'

Common Mistake

  • Beginners often forget to unpack both index and value from enumerate(). Always use: for i, val in enumerate(...)

Interview Tip

enumerate() is often preferred in interviews over manually tracking counters in loops. It shows clean, Pythonic style.

Summary

  • enumerate() helps track both index and item while looping.
  • You can change the starting index using the start argument.
  • Useful for iterating through lists, strings, or any iterable.

Practice Problem

Write a program that takes a list of names and prints them numbered from 1:

names = ["Alice", "Bob", "Charlie"]
for i, name in enumerate(names, start=1):
    print(f"{i}. {name}")

Expected Output:

1. Alice
2. Bob
3. Charlie

What to remember

Connect what you noticed in the example with the idea below.

Remember

enumerate() yields index/value pairs. The optional start changes the first index, not the items.

Watch out for this idea

The start argument skips that many items.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →