Python enumerate() Function Tutorial

In this section, we will learn what the enumerate() method is and how to use it in Python.

Enumeration in Python: What Does Enumeration Mean?

The Python enumerate() function is used to enumerate through the elements of an iterable object and return those elements as a result.

The beauty of this function is that it will attach the index number of each element within the target iterable object so you can see the index number of the element as well.

Python enumerate() Function Syntax:

enumerate(iterableObject)

Python enumerate() Function Parameters

The function takes one argument and that is a reference to the iterable object that we want to get its elements (items).

Python enumerate() Function Return Value

The return value of this function is an Enumerate object that contains each item and its index position of the target iterable object.

Example: for enumerate in python

dictionary = {
    "name": "John",
    "lastName": "Doe",
    "age": 200
}

enum = enumerate(dictionary)
for index, element in enum:
    print(index,element)

Output:

0 name

1 lastName

2 age

Note that invoking the enumeration() function in a dictionary will enumerate through its keys and not values.

Example: python enumerate dictionary

string = "This is a simple sentence"

enum = enumerate(string)
for index, element in enum:
    print(index,element)

Output:

0 T

1 h

2 i

3 s

4

5 i

6 s

7

8 a

9

10 s

11 i

12 m

13 p

14 l

15 e

16

17 s

18 e

19 n

20 t

21 e

22 n

23 c

24 e
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies