How to Use Enumerate In PYTHON?

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.

Take the quiz →

Examples

  1. Using enumerate to count the number of fruits in a list, like counting apples and oranges.
  2. Looping through a list of names with numbers using enumerate.
  3. Adding indexes to items in a shopping list with enumerate.

Ask a question

See also

Discussion

Recent activity

Categories: Science · Python· Programming· Enumerate