ennumerate python code example

Example 1: 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 2: enumerate python

for index,char in enumerate("abcdef"):
  print("{}-->{}".format(index,char))
  
0-->a
1-->b
2-->c
3-->d
4-->e
5-->f

Example 3: ennumerate python

grocery = ['bread', 'milk', 'butter']

for item in enumerate(grocery):
  print(item)
''' output
(0, 'bread')
(1, 'milk')
(2, 'butter')
'''

for count, item in enumerate(grocery):
  print(count, item)
''' output
0 bread
1 milk
2 butter
'''

# changing default start value
for count, item in enumerate(grocery, 100):
  print(count, item)
''' output
100 bread
101 milk
102 butter
'''