Answers for "ennumerate python"

82

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)
Posted by: Guest on May-02-2020
11

enumerate python

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

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
'''
Posted by: Guest on April-27-2021

Python Answers by Framework

Browse Popular Code Answers by Language