fruits_list = ["Apple", "Orange", "Banana", "Pineapple"]
#using enumerate() method
for index, value in enumerate(fruits_list):
print(index, value)
#using range() method
for index in range(len(fruits_list)):
print(index, fruits_list[index])
In the code snippet, we have a list named fruits_list and we are iterating it using for loop. If we want to access the iteration index inside for loop, we are using enumerate() function for that.
You can define your index to start from another number in place of 0. you just need to pass the start parameter value inside enumerate() method. Below is the code example for that.
fruits_list = ["Apple", "Orange", "Banana", "Pineapple"]
for index, value in enumerate(fruits_list, start=10):
print(index, value)
my_list = ["Math", "Physics", "Chemistry"]
for index in range(len(my_list)):
print(my_list[index])
# Output:
# Math
# Physics
# Chemistry
0 Comments