python get index and value from list in a for loop code example
Example 1: python how to get index in for loop
index = 0
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for food in foods:
print("Food", index, "is", food)
index += 1
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for index, value in enumerate(foods):
print("Food", index, "is", value)
Example 2: get index of list item in loop
ints = [1,2,3,4,'5','6']
for idx, val in enumerate(ints):
print(idx, val)
0 1
1 2
2 3
3 4
4 '5'
5 '6'