python enumerate loop code example

Example 1: python3 iterate through indexes

items=['baseball','basketball','football']
for index, item in enumerate(items):
    print(index, item)

Example 2: python for with iterator index

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

Example 3: enumerate in python

languages = ['Python', 'C', 'C++', 'C#', 'Java']

#Bad way
i = 0 #counter variable
for language in languages:
    print(i, language)
    i+=1

#Good Way
for i, language in enumerate(languages):
    print(i, language)

Example 4: python gt index in for cycle

my_list = [0,1,2,3,4]
for idx, val in enumerate(my_list):
    print('{0}: {1}'.format(idx,val))
#This will print:
#0: 0
#1: 1
#2: 2
#...

Example 5: enumerate python

for index,subj in enumerate(subjects):
    print(index,subj) ## enumerate will fetch the index
    
0 Statistics
1 Artificial intelligence
2 Biology
3 Commerce
4 Science
5 Maths

Example 6: for loop with index python3

colors = ["red", "green", "blue", "purple"]

for i in range(len(colors)):
    print(colors[i])