Indexes and Values (enumerate() function


Thu Nov 25 2021 14:37:58 GMT+0000 (UTC)

Saved by @Sourabh #numpy #booleans_in_numpy

# areas list
areas = [11.25, 18.0, 20.0, 10.75, 9.50]

# Change for loop to use enumerate() and update print()
for index, area in enumerate(areas) :
    print("room " + str(index) + ": " + str(area))


Using a for loop to iterate over a list only gives you access to every list element in each run, one after the other. If you also want to access the index information, so where the list element you're iterating over is located, you can use enumerate().