Skip to content

Latest commit

 

History

History
21 lines (14 loc) · 550 Bytes

20220905182723.md

File metadata and controls

21 lines (14 loc) · 550 Bytes

Get a loop counter with enumerate()

Python's enumerate() built-in function is perfect for iteration with indexing.

Forget about manually incrementing indexes; with enumerate(), you can start the index at any number using the (optional) "start" keyword. 😍

No more index += 1, use enumerate()!

nba_players = ["LeBron James", "Kevin Durant", "Stephen Curry"]
for index, player in enumerate(nba_players, start=1):
    print(f"{index}: {player}")

# Outputs:
# 1: LeBron James
# 2: Kevin Durant
# 3: Stephen Curry

#built-ins #enumerate