Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Understanding the Python enumerate() method

Python Enumerate()

Introduction

Today in this tutorial, we are going to learn about the Python enumerate() method.

The Python enumerate() Method

The Python’s built-in enumerate() method converts a passed sequence into an enumerate object having the same elements in the form of tuple. Further, the function adds index to the corresponding tuple elements.

The syntax for using the Python enumerate() method is,

enumerate( thing, start)

Here,

  • thing is any sequence for which we need to add individual element index,
  • start(optional) is the starting value with which the indexing would start. If not passed, default value is set to 0.

Using the Python enumerate() Method

The Python enumerate() method can convert any iterable sequence into a enumerate object with added indices. This sequence can be a list, string, or a tuple. But it is not allowed to be a dictionary or a set, as they are not sequences.

Now let us look at some examples to have a better understanding.

Enumerate a List

When we pass a list to the Python enumerate() method, it adds the elementary indices as the first value into the tuple element. The returned iterable is of type enumerate.

To print this enumerate object, we can use a simple for loop traversing through it. Note, this time we can access both the element index and element value at the same time.

list1 = [ 11, 22, 33, 44]

enumerate_list = enumerate(list1)
print("Enumerated list now looks like: ", list(enumerate_list))

#print the index and corresponding value for enumerated list1
for i, item in enumerate(list1):
    print("Index = ", i,"  :  ","value = ",item)

Output:

Enumerated list now looks like:  [(0, 11), (1, 22), (2, 33), (3, 44)]
Index =  0   :   value =  11
Index =  1   :   value =  22
Index =  2   :   value =  33
Index =  3   :   value =  44

Here,

  • list1 is a list with some initial values in it. We pass it to the enumerate() method and store the returned object inside the enumerate_list variable,
  • When we type-caste this object into a list and try to print it using the print() method, we can clearly observe that each element of the list is now converted into a tuple with added index,
  • Nearly we use for loop with two variables i and item to traverse through the enumerated object. In this way, we can access both index(i) as well as the corresponding element (item) at the same time.

Hence, the output is justified.

The enumerate() works in the same way for tuples too.

Enumerate a String

Similarly, we can also use the Python enumerate() method to convert a string into an enumerate object with added indices.

Let us see how.

string1 = "AskPython"

enumerate_string = enumerate(string1)
print("Enumerated list now looks like: ", list(enumerate_string))

#print the index and corresponding character for enumerated string
for i, item in enumerate(string1):
    print("Index = ", i,"  :  ","character = ",item)

Output:

Enumerated list now looks like:  [(0, 'A'), (1, 's'), (2, 'k'), (3, 'P'), (4, 'y'), (5, 't'), (6, 'h'), (7, 'o'), (8, 'n')]
Index =  0   :   character =  A
Index =  1   :   character =  s
Index =  2   :   character =  k
Index =  3   :   character =  P
Index =  4   :   character =  y
Index =  5   :   character =  t
Index =  6   :   character =  h
Index =  7   :   character =  o
Index =  8   :   character =  n

Here,

  • We initialize a string string1 and store its enumerate() output in a variable enumerate_string,
  • Then the list type-casted enumerate_string is printed. As we can see it is a list of tuples containing individual character elements with their respective indices,
  • Again we traverse through the enumerate object using a for loop and print out the elements with indices.

Python enumerate() with Start parameter

As mentioned earlier, the start parameter is an optional parameter that determines from which value the indexing would start for the enumerate object returned by the enumerate() method.

Let us look at an example where we try to change the indexing of a list with starting index 0 to a list with starting index 20.

list1 = [ 11, 22, 33, 44]

enumerate_list = enumerate(list1)
print("Enumerated list now looks like: ", list(enumerate_list))

#without start
print("Without Start:")
for i, item in enumerate(list1):
    print("Index = ", i,"  :  ","value = ",item)

#with start = 20
print("With Start:")
for i, item in enumerate(list1, 20):
    print("Index = ", i,"  :  ","value = ",item)

Output:

Enumerated list now looks like:  [(0, 11), (1, 22), (2, 33), (3, 44)]
Without Start:
Index =  0   :   value =  11
Index =  1   :   value =  22
Index =  2   :   value =  33
Index =  3   :   value =  44
With Start:
Index =  20   :   value =  11
Index =  21   :   value =  22
Index =  22   :   value =  33
Index =  23   :   value =  44

Here, it is clear from the output that with start=20, the starting index of the enumerate object returned by the method is 20. Whereas without start(default value 0) the indexing starts from 0.

Conclusion

Note that the Python enumerate() method only works for sequences. Hence, dictionaries or sets cannot be converted into enumerate() objects.

So, in this tutorial, we learned about the enumerate() method in Python. For any further questions, feel free to ask in the comments.

References