Enumerate In Python

By | July 2, 2021
Enumerate In Python

In Python when we loop over a list or array we generally use the for loop iterator and iterate over the list’s elements one by one. But many times, we come across the logic where we want the list elements and their index counter value. In that case, we can either use the range function with list length or Python’s inbuilt enumerate function.

In this article, we will discuss the Python enumerate function and how to use it.

Vamware

Enumerate() in Python

Enumerate is an inbuilt Python function that accepts an iterable object and returns an enumerate object which is a collection of pair tuples of iterable elements and their counter (index) value.

Syntax

enumerate(iterablestart)
  • The iterable could be  a list, string, tuple, dictionary, or set
  • The start parameter is optional and it represents the counter number. By default, its value is 0.

The enumerate function returns a enumerate iterable object.

Example

>>> fruits = ['apple', 'orange', 'grapes', 'banana']
>>> enumerate(fruits)
<enumerate object at 0x0000020C8E809AC0>
The enumerate object returned by the enumerate function is iterable and can be iterate using for loop.
fruits = ['apple', 'orange', 'grapes', 'banana']

for index, value in enumerate(fruits):
    print(index, value)

Output

0 apple
1 orange
2 grapes
3 banana

We can also specify the start parameter to the enumerate function and the counter index value will start from that specified start number.

Example

fruits = ['apple', 'orange', 'grapes', 'banana']

#start the counter value from 1
for index, value in enumerate(fruits, start=1):
    print(index, value)

Output

1 apple
2 orange
3 grapes
4 banana

Enumerate Object

The enumerate object returned by the enumerate function include the iterable object in the following order

(0, element1), (1, element2), (2, element3), (3, element4)...

As the enumerate object is iterable we can use the for loop to access all the elements of the object.

We can also use the type conversion and convert the enumerate object into a Python list using list() function. With that, we can easily check how the enumerate object saves elements.

Example

fruits = ['apple', 'orange', 'grapes', 'banana']
enum_obj = enumerate(fruits)

#convert enumerate object to a list
my_bucket = list(enum_obj)

print(my_bucket)

Output

[(0, 'apple'), (1, 'orange'), (2, 'grapes'), (3, 'banana')]

Enumerate Python List

Python list is also an iterable object and it can be enumerated using the Python enumerate function.

When we enumerate a list using enumerate function, we get an enumerated iterable object which is a collection of index and element values tuples.

Example

fruits = ['apple', 'orange', 'grapes', 'banana']

for index, element in enumerate(fruits):
    print("Index:",index, "Element:", element )

Output

Index: 0 Element: apple
Index: 1 Element: orange
Index: 2 Element: grapes
Index: 3 Element: banana

Enumerate Python Tuple

A tuple is an immutable data container that stores elements in sequential order. Similar to the Python list, we can enumerate Python tuple elements with their corresponding index value.

Example

#tuple
fruits = ('apple', 'orange', 'grapes', 'banana')

for index, element in enumerate(fruits):
    print("Index:",index, "Element:", element )

Output

Index: 0 Element: apple
Index: 1 Element: orange
Index: 2 Element: grapes
Index: 3 Element: banana

Enumerate Python String

A string is a collection of characters and it also stores its all character in sequential order. The string is also an iterable object and can be enumerated using the Python enumerate function.

Example

#given string
string = "Hello World!"

for index,character in enumerate(string):
    print("Index:",index, "Character:", character )

Output

Index: 0 Character: H
Index: 1 Character: e
Index: 2 Character: l
Index: 3 Character: l
Index: 4 Character: o
Index: 5 Character: 
Index: 6 Character: W
Index: 7 Character: o
Index: 8 Character: r
Index: 9 Character: l
Index: 10 Character: d
Index: 11 Character: !

Enumerate Python Dictionary

When we perform the enumerate()function on a dictionary object, the function enumerates the dictionary key’s not the values.

Example

#given dictionary
my_dict={"key1":"value1", "key2": "value2", "key3":"value3"}

for index,key in enumerate(my_dict):
    print("Index:",index, "Key:", key )

Output

Index: 0 Key: key1
Index: 1 Key: key2
Index: 2 Key: key3

To enumerate dictionary values we can use the dictionary values() function.

Example

#given dictionary
my_dict={"key1":"value1", "key2": "value2", "key3":"value3"}

for index,value in enumerate(my_dict.values()):
    print("Index:",index, "Value:", value )

Output

Index: 0 Value: value1
Index: 1 Value: value2
Index: 2 Value: value3

Enumerate Python Set

A Python set is a collection of unique elements, and it neither stores its elements in sequential order nor it gives index values to its elements.

Like tuple, and list, Python sets are also iterable objects and we can enumerate them using enumerate function.

Example

#given set
fruits = {'apple', 'orange', 'grapes', 'banana'}

for counter, element in enumerate(fruits):
    print("Counter:",counter, "Element:", element )

Output

Counter: 0 Element: grapes
Counter: 1 Element: banana
Counter: 2 Element: orange
Counter: 3 Element: apple

Conclusion

With this let’s conclude our article on Python enumerate function. The enumerate function comes very handy when we want to grab the index number and element of a list or tuple at the same time inside a for loop. The enumerate function just assigns a counter integer number to the iterable object elements and returns a enumerate object.

Leave a Reply

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