Enumerate is a tool in Python that helps you count things as you go through them, like counting your toys while putting them away.
Imagine you have a line of toy cars: red, blue, green, and yellow. You want to know not just their colors but also their order, first, second, third, fourth. That’s where enumerate comes in handy.
How Enumerate Works
Let's say you have this list:
cars = ["red", "blue", "green", "yellow"]
Now use enumerate like this:
for index, color in enumerate(cars):
print(f"Car number {index} is {color}.")
This will give you:
- Car number 0 is red.
- Car number 1 is blue.
- Car number 2 is green.
- Car number 3 is yellow.
It’s like having a little counter that starts at 0 and goes up by 1 each time, just like counting your steps as you walk to the park!
You can even start the count from 1 instead of 0 if you want, just like starting your toy car race from lane 1.
Examples
- Adding indexes to items in a shopping list with
enumerate.
Ask a question
See also
- How Does Python Enumerate Function - Python Quick Tips Work?
- How Does Beginner Python Tutorial 84 - Arguments and Parameters Work?
- How Does asyncio in Python - Async/Await Work?
- How Does 76 What is Exception in Java Work?
- How Does Elixir in 100 Seconds Work?