Enumerate In Python: Iterables, Iterators, and the Eumerate Function

Posted in /  

Enumerate In Python: Iterables, Iterators, and the Eumerate Function
vinaykhatri

Vinay Khatri
Last updated on March 19, 2024

    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 scenarios where we want the list elements and their index values. 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 enumerate in Python and how to use it.

    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.

    Code Syntax

    enumerate(iterable, start)
    • 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.

    Code 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 iterated 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.

    Code 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 includes 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 type conversion and convert the enumerate object into a Python list using the list() function. With that, we can easily check how the enumerate object saves elements.

    Code 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 List

    Python list is also an iterable object, and it can be enumerated using the Python enumerate function. When we enumerate a list using the enumerate function, we get an enumerated iterable object, which is a collection of index and element values tuples.

    Code 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 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 values.

    Code 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 String

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

    Code 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 keys and not the values.

    Code 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.

    Code 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 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 the enumerate function.

    Code 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 the Python enumerate function. The enumerate function comes very handily 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 an enumerate object.

    People are also reading:

    Leave a Comment on this Post

    0 Comments