Enumerate Loop in Python

Level 391 keystrokes

This prints each color with its index. enumerate() provides both the index and value while iterating.

Python code

# Create a list of color names
1colors = ["red", "green", "blue"]
# enumerate() returns pairs of (index, value)
# i gets the index, color gets the value
2for i, color in enumerate(colors):
# Print index and color together
# First: "0: red", then "1: green", then "2: blue"
3 print(f"{i}: {color}")

Output

0: red
1: green
2: blue
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 8
  • ,× 3
  • :× 2
  • (× 2
  • )× 2
  • {× 2
  • }× 2
  • [× 1
  • ]× 1
  • =× 1

More Python examples at level 3

← Python typing practice