tgoop.com/DataScience4/8510
Create:
Last Update:
Last Update:
In Python, enhanced for loops with enumerate() provide both the index and value of items in an iterable, making it ideal for tasks needing positional awareness without manual counters. This is more Pythonic and efficient than using range(len()) for list traversals.
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# Output:
# 0: apple
# 1: banana
# 2: cherry
# With start offset:
for index, fruit in enumerate(fruits, start=1):
print(f"{index}: {fruit}")
# 1: apple
# 2: banana
# 3: cherry
#python #forloops #enumerate #bestpractices
