Using enumerate() in Comprehensions
enumerate() provides both the index and value while iterating, useful for index-aware transformations.
enumerate() provides both the index and value while iterating, useful for index-aware transformations.
fruits = ["apple", "banana", "cherry"]
indexed = {i: fruit for i, fruit in enumerate(fruits)}
print(indexed)
# {0: "apple", 1: "banana", 2: "cherry"}
Pass start=1 to enumerate() to begin counting from 1.