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)
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)
enumerate python
for index,char in enumerate("abcdef"):
print("{}-->{}".format(index,char))
0-->a
1-->b
2-->c
3-->d
4-->e
5-->f
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
'''
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us