Enumerate function in python

what is the enumerate function in python ?

Enumerate function is a built-in function in Python.A lot of times when you dealing with iterators then we need to keep a count of each iterators. But in python have a in-built function enumerate object method adds counter to an iterable and return it. Enumerate is very helpful in such a case as it gives the index and items at one go.

The syntax of enumerate() is :

enumerate(iterable, startIndex)

iterableAn iterable object
startA Number. Define the start number of the enumerate object. By Default start from 0

How to use enumerate in python and Enumerate apply on python lists because lists are one type of iterable object.

l1 = ["python","topic","should",'know']
obj1 = enumerate(l1)

#output
[(0, 'python'), (1, 'topic'), (2, 'should'), (2, 'know')]
fruits = [ "Apple","Berry","Cherry"]
for i,j in enumerate(fruits):
    print(i,j)

#output
0 Apple
1 Berry
2 Cherry

UsingEnumerate() on a list with startIndex :

mylist = ['A', 'B' ,'C', 'D']
e_list = enumerate(mylist,2)
print(list(e_list))

#output
[(2, 'A'), (3, 'B'), (4, 'C'), (5, 'D')]

Looping Over an Enumerate object:

mylist = ['A', 'B' ,'C', 'D']
print("Using startIndex as 10")

for i in enumerate(mylist):
  print(i)
  print("\n")

print("Using startIndex as 10")    

for i in enumerate(mylist, 10):
  print(i)
  print("\n")

#output
(0, 'A')
(1, 'B')
(2, 'C')
(3, 'D')

#Using startIndex as 10

(10, 'A')
(11, 'B')
(12, 'C')
(13, 'D')

Enumerating a Tuple:

you can use tuple in enumerate with starting index and key of each item will start form the starting index and by default start index is 0.

#Convert a tuple into an enumerate object:

x = ('apple', 'banana', 'cherry')
y = enumerate(x)
print(list(y))

#output
[(0, 'apple'), (1, 'banana'), (2, 'cherry')]

fruits = [(15,"Fifteen"), (12,"Twelve"), (19,"Nineteen")]
for i,itemm in enumerate(fruits):
    print(i,itemm)

#output
0 (15, 'Fifteen')
1 (12, 'Twelve')
2 (19, 'Nineteen')


my_tuple = ("A", "B", "C", "D", "E")
for i in enumerate(my_tuple):
  print(i)

#output
(0, 'A')
(1, 'B')
(2, 'C')
(3, 'D')
(4, 'E')

Enumerating a String :

you can apply enumerate on python strings, the function take string as an array and apply loop over it.when you pass a string in enumerate the output will show you index and value of each character of the string.

my_str = "MYSTRING"
for i in enumerate(my_str):
  print(i)

#output
(0, 'M')
(1, 'Y')
(2, 'S')
(3, 'T')
(4, 'R')
(5, 'I')
(6, 'N')
(7, 'G')

Enumerate on Different Starting Index :

lang = "LANGUAGE"
for i,j in enumerate(lang, start=2):
    print(i,j)

#output
2 L
3 A
4 N
5 G
6 U
7 A
8 G
9 E

Enumerate Dictonaries :

It has no sense enumerate on dictionary because dictionary is not a sequence. The dictionary have not any index. it is always in same order.

my_dict = {"a": "PYTHON", "b":"great", "c":"Language", "d":"World"}
for i in enumerate(my_dict):
  print(i)

#output
(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')

If you like the article share with your friends and you have any question or query related to article and any suggestions please leave in comment box.

Thanks for reading the Article.

Leave a Reply

Your email address will not be published. Required fields are marked *