| by Arround The Web | No comments

Enumerate python examples

Python provides various functions and modules for different aspects, including simple to complex tasks. For instance, to loop over a collection and keep track of the indexes of the items. In such a case, the “enumerate()” function comes into effect that is used to retrieve the specified count value along with the item value.

This tutorial will explain a comprehensive guide on the “enumerate()” function using the below content:

What is the Python “enumerate()” Function?

The “enumerate()” function is utilized in Python to count the items in an iterable such as a list, tuple, or others and return the count and the item together.

Syntax

enumerate(iterable, start=0)

 
Parameters

In the above syntax:

    • The “iterable” parameter specifies an iterator object.
    • The optional “start” parameter specifies the “index” value from where the counter needs to start.
    • The “standard/default” value is “0”.

Return Value

The “enumerate()” function retrieves an enumerate object.

Example 1: Applying the “enumerate()” Function to the Specified Iterator Object

The following example code demonstrates the working of the Python “enumerate()” function on a target object:

list_name = ['Joseph', 'Anna', 'Henry']
enum_obj = enumerate(list_name)
print(enum_obj, '\n')
print(list(enum_obj))

 
In the above code:

    • The list “list_name” is created.
    • The “enumerate()” function takes the list “list_name” as an argument and retrieves the enumerated object that contains a default counter along with an iterable value.
    • The enumerated object is then transformed into a list via the “list()” function.

Output


The enumerated object containing the iterable value along with the default counter has been shown in the above output.

Example 2: Applying the “enumerate()” Function to Iterator Object With the Specified Counter Value

The below code is used to add the specified counter value to the enumerated object:

list_name = ['Joseph', 'Anna', 'Henry']
enum_obj = enumerate(list_name, 10)
print(enum_obj, '\n')
print(list(enum_obj))

 
In the above code lines, the default counter value of the “enumerate()” function is changed to the specified counter value “10” to start the counter from that value.

Output


The enumerated object with a specified start counter value has been displayed.

Example 3: Applying the “enumerate()” Function With “for” Loop to Loop/Iterate Through the Enumerate Object

The “for” loop iterates through the enumerated object. Here is an example code:

list_name = ['Joseph', 'Anna', 'Henry']
for count, i in enumerate(list_name):
  print(count, i)
print()

 
In this code, the “for” loop is used to iterate through the enumerated object and show the iterable value along with the default counter using the “print()” function.

Output


Based on the above output, the enumerated object has been iterated successfully.

Example 4: Applying the “enumerate()” Function With “for” Loop to Iterate/Loop Over the Enumerate Object With the Specified Counter Value

The “for” loop can also iterate the enumerated object starting from the specified counter value. Here is an example:

list_name = ['Joseph', 'Anna', 'Henry']
for count, i in enumerate(list_name, 10):
  print(count, i)
print()

 
In the above code block, the specified counter value “10” is passed to the “enumerate()” function to start counting values from the specified counter till the end.

Output


Based on the above output, the enumerated object has been iterated and counted from the specified value.

Conclusion

The “enumerate()” function in Python is utilized to count the elements of the specified iterable and return the count and the item together. This function retrieves an enumerated object that contains a counter with items/elements of the iterator. The counter value can also be modified and passed to the “enumerate()” function. This post presented a detailed guide on the “enumerate()” function using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply