1items=['baseball','basketball','football']
2for index, item in enumerate(items):
3 print(index, item)
1for index, item in enumerate(items):
2 print(index, item)
3
4#if you want to start from 1 instead of 0
5for count, item in enumerate(items, start=1):
6 print(count, item)
1my_list = [0,1,2,3,4]
2for idx, val in enumerate(my_list):
3 print('{0}: {1}'.format(idx,val))
4#This will print:
5#0: 0
6#1: 1
7#2: 2
8#...
1# There are two ways to do this
2# The "beginner" one:
3index = 0
4foods = ["burger", "pizza", "apple", "donut", "coconut"]
5for food in foods:
6 print("Food", index, "is", food)
7 index += 1
8
9# Or using enumerate:
10foods = ["burger", "pizza", "apple", "donut", "coconut"]
11for index, value in enumerate(foods):
12 print("Food", index, "is", value)
13
14# By convention, you should understand and use the enumerate function as it makes the code look much cleaner.
1colors = ["red", "green", "blue", "purple"]
2
3for i in range(len(colors)):
4 print(colors[i])
5