Python Tutorial 29 --- "Enumerate Function"
Python Tutorial 29 --- "Enumerate Function"
“ An enumerate is a built-in function that provides an index to data structure elements, making iterations through them easier and simpler.”
In previous tutorials we have seen many methods that print items of different data structures onto the screen. For these kinds of operations, we need to have a for loop along with an iterator and an integer variable in which we have to increment every time the loop runs. Well, python provides us with a simple and easy solution to deal with certain situations by providing us with a built-in function known as “enumerate function.” Using the enumerate function, we can summarize the code and make it easier and simpler to use. It is really important to know the concept of Enumerate function so that you can apply this concept to your code.
Let us understand the working of enumerate function:
What enumerate function does is, it assigns an index to every element or value in the object that we want to iterate, so we do not have to assign a specific variable for incremental function, instead we have to apply a for loop, and our function will start working. Its syntax is a lot simpler and shorter than what we have been following till now.
Syntax
When calling a simple enumeration function, we have to provide two parameters:
- The data structure that we want to iterate
- The index from where we want to start our iteration
Note: The iterable must be an object that supports iteration
Example of enumerate using a python list.
We can iterate over the index and value of an item in a list by using a basic for loop with enumerate().
Output:
Using Enumerate() on a list with start Index:
In the below example, the starting index is given as 5. The index of the first item will start from the given starting index.
Example:
We will get the following output:
If we do not provide the index, we want to start the iteration from then it automatically starts its iteration from zero index i.e., the beginning of the data structure.
Instead of returning a string, the enumerate function returns an object by adding the iterating counter value. We can also convert the enumerator object into a list(), tuple(), set(), and many more.
Advantages of using Enumerate:
- It is a built-in function
- It makes the code shorter
- We do not have to keep count of the number of iterations
- It makes the implementation of for loop simpler and cleaner
- Lesser code so lessor chances of error and bugs
- We can loop through string, tuple or objects using enumerate
- We can start the iteration from anywhere within the data structure as we have the option of providing the starting index for iteration.
CODE-
# Enumerate Function
l1 = ["Iron Man", "Captain America", "Thor", "Black Widow", "Hulk", "Hawkeye", "Ant-man"]
# Normal Method
# i=1
# for items in l1:
# if i%2 != 0:
# print(items)
# i+=1
# Enumerate Method
for index, items in enumerate(l1):
if index % 2 == 0:
print(items)
Comments
Post a Comment