xxxxxxxxxx
>>> frutas = ["manzanas", "peras", "naranjas"]
>>> for i, fruta in enumerate(frutas):
print(i, fruta)
0 manzanas
1 peras
2 naranjas
xxxxxxxxxx
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)
xxxxxxxxxx
for index,subj in enumerate(subjects):
print(index,subj) ## enumerate will fetch the index
0 Statistics
1 Artificial intelligence
2 Biology
3 Commerce
4 Science
5 Maths
xxxxxxxxxx
fruits = ['Banana', 'Apple', 'Lime']
list(enumerate(fruits))
# Output
# [(0, 'Banana'), (1, 'Apple'), (2, 'Lime')]
xxxxxxxxxx
for index,char in enumerate("abcdef"):
print("{}-->{}".format(index,char))
0-->a
1-->b
2-->c
3-->d
4-->e
5-->f
xxxxxxxxxx
rhymes=['check','make','rake']
for rhyme in enumerate(rhymes):
print(rhyme)
#prints out :
(0, 'check')
(1, 'make')
(2, 'rake')
#basically just prints out list elements with their index
xxxxxxxxxx
#Enumerate in python
l1 = ['alu','noodles','vada-pav','bhindi']
for index, item in enumerate(l1):
if index %2 == 0:
print(f'jarvin get {item}')
xxxxxxxxxx
# printing the tuples in object directly
for ele in enumerate(["eat", "sleep", "code"]):
print (ele)
>>>(0, 'eat')
>>>(1, 'sleep')
>>>(2, 'code')
# changing index and printing separately
for count, ele in enumerate(["eat", "sleep", "code"], 100):
print (count, ele)
>>>100 eat
>>>101 sleep
>>>102 code
# getting desired output from tuple
for count, ele in enumerate(["eat", "sleep", "code"]):
print(count)
print(ele)
>>>0
>>>eat
>>>1
>>>sleep
>>>2
>>>code
xxxxxxxxxx
for key, value in enumerate(["p", "y", "t", "h", "o", "n"]):
print key, value
"""
0 p
1 y
2 t
3 h
4 o
5 n
"""
xxxxxxxxxx
list1 = ['1', '2', '3', '4']
for index, listElement in enumerate(list1):
#What enumerate does is, it gives you the index as well as the element in an iterable
print(f'{listElement} is at index {index}') # This print statement is just for example output
# This code will give output :
"""
1 is at index 0
2 is at index 1
3 is at index 2
4 is at index 3
"""