Replace C-style for loops in Python with enumerate()

By: fyvo August 4, 2025 Python

Description

Many developers coming from other languages use for i in range(len(...)). Using enumerate() makes the code cleaner and more readable.

Original Code (Outdated)

items = ["apple", "banana", "cherry"]
for i in range(len(items)):
    print(i, items[i])

Updated Code (Modern)

items = ["apple", "banana", "cherry"]
for i, item in enumerate(items):
    print(i, item)

Discussion (0)