when to use enumerate in python code example
Example 1: enumerate in python
languages = ['Python', 'C', 'C++', 'C#', 'Java']
i = 0
for language in languages:
print(i, language)
i+=1
for i, language in enumerate(languages):
print(i, language)
Example 2: enumerate python
grocery = ['bread', 'milk', 'butter']
for item in enumerate(grocery):
print(item)
print('\n')
for count, item in enumerate(grocery):
print(count, item)
print('\n')
for count, item in enumerate(grocery, 100):
print(count, item)
OUTPUT:
(0, 'bread')
(1, 'milk')
(2, 'butter')
0 bread
1 milk
2 butter
100 bread
101 milk
102 butter