1languages = ['Python', 'C', 'C++', 'C#', 'Java']
2
3#Bad way
4i = 0 #counter variable
5for language in languages:
6 print(i, language)
7 i+=1
8
9#Good Way
10for i, language in enumerate(languages):
11 print(i, language)
12
1>>> for count, value in enumerate(values):
2... print(count, value)
3...
40 a
51 b
62 c
7
1>>> values = ["a","b","c"]
2>>> for count, value in enumerate(values):
3... print(count, value)
4...
50 a
61 b
72 c
8
1rhymes=['check','make','rake']
2for rhyme in enumerate(rhymes):
3 print(rhyme)
4#prints out :
5(0, 'check')
6(1, 'make')
7(2, 'rake')
8#basically just prints out list elements with their index
1for key, value in enumerate(["p", "y", "t", "h", "o", "n"]):
2 print key, value
3
4"""
50 p
61 y
72 t
83 h
94 o
105 n
11"""
12
1grocery = ['bread', 'milk', 'butter']
2
3for item in enumerate(grocery):
4 print(item)
5
6print('\n')
7for count, item in enumerate(grocery):
8 print(count, item)
9
10print('\n')
11# changing default start value
12for count, item in enumerate(grocery, 100):
13 print(count, item)
14
15OUTPUT:
16(0, 'bread')
17(1, 'milk')
18(2, 'butter')
19
200 bread
211 milk
222 butter
23
24100 bread
25101 milk
26102 butter