for i in list python index code example

Example 1: python for with iterator index

for index, value in enumerate(iterator):
    print(index, value)

Example 2: python how to get index in for loop

# There are two ways to do this
# The "beginner" one:
index = 0
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for food in foods:
  print("Food", index, "is", food)
  index += 1

# Or using enumerate:
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for index, value in enumerate(foods):
    print("Food", index, "is", value)

# By convention, you should understand and use the enumerate function as it makes the code look much cleaner.

Example 3: find an index of an item in a list python

#Example List
list = ['apples', 'bannas', 'grapes']
#Use Known Entites In The List To Find The Index Of An Unknown Object
Index_Number_For_Bannas = list.index('apples')
#Print The Object
print(list[Index_Number_For_Bannas])

Example 4: python iterate with index

for index, item in enumerate(iterable, start=1):
   print index, item

Example 5: how to index lists in python

# Make the list
list = ['bananas', 'apples', 'watermellon']
# Print the word apples from the list
print(list[1])