Python
15 Best Web Scraping Tools for Data Extraction in 2021
{loadposition top-ads-automation-testing-tools} Web scraping tools are specially developed...
Python Enumerate() is a buit-in function available with the Python library. It takes the given input as a collection or tuples and returns it as an enumerate object. The Python Enumerate() command adds a counter to each item of the iterable object and returns an enumerate object as an output string.
In this Enumerate Python tutorial, you will learn:
enumerate(iterable, startIndex)
Three parameters are:
However, If startIndex is not specified, the count will start from 0.
It will return an iterableobject, with countvalue to each of the items to the iteratorobject given as input.
Enumerate method comes with an automatic counter/index to each of the items present in the Enumerate list in Python. The firstindex value will start from 0. You can also specify the startindex by using the optional parameter startIndex in enumerate.
In the code below, mylist is the list given to Enumerate function in Python. The list() function is used to display the Enumerate Python output.
Note: There is no startIndex used hence the index for the firstitem will start from 0.
The output from enumerate will be in the following manner:
(0, item_1), (1, item_2), (2, item_3), … (n, item_n)
File: python_enumerate.py
mylist = ['A', 'B' ,'C', 'D'] e_list = enumerate(mylist) print(list(e_list))
Output:
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
In the below example, the startindex given as 2.The index of the firstitem will start from the given startindex.
Example:
In the example below, mylist is the list given to enumerate. The list() function is used to display the enumerate output.
mylist = ['A', 'B' ,'C', 'D'] e_list = enumerate(mylist,2) print(list(e_list))
Output:
[(2, 'A'), (3, 'B'), (4, 'C'), (5, 'D')]
The example shows enumerating over an object with and without startIndex.
Example:
mylist = ['A', 'B' ,'C', 'D']
for i in enumerate(mylist):
print(i)
print("\n")
print("Using startIndex as 10")
for i in enumerate(mylist, 10):
print(i)
print("\n")
Output:
(0, 'A') (1, 'B') (2, 'C') (3, 'D') Using startIndex as 10 (10, 'A') (11, 'B') (12, 'C') (13, 'D')
In the below example, you can use a tuple inside an enumerate. You can also use a startIndex, and the key to each item will start from the startIndexgiven.
By default, the startIndex is 0. There, hence you see key as 0 for items A and 1 for B and so on.
Example:
my_tuple = ("A", "B", "C", "D", "E")
for i in enumerate(my_tuple):
print(i)
Output:
(0, 'A') (1, 'B') (2, 'C') (3, 'D') (4, 'E')
In Python, the string is an array, and hence you can loop over it. If you pass a string to enumerate(), the output will show you the index and value for each character of the string.
Example:
my_str = "gtupapers " for i in enumerate(my_str): print(i)
Output:
(0, 'G') (1, 'u') (2, 'r') (3, 'u') (4, '9') (5, '9')
In Python, a dictionary is listed in curly brackets, inside these curly brackets, the values are declared.
Each element is a key/value pair and separated by commas. You can use a dictionary inside a enumerate() and see the output.
my_dict = {"a": "PHP", "b":"JAVA", "c":"PYTHON", "d":"NODEJS"}
for i in enumerate(my_dict):
print(i)
Output:
(0, 'a') (1, 'b') (2, 'c') (3, 'd')
Here, are pros/benefits of using Enumerate in Python:
{loadposition top-ads-automation-testing-tools} Web scraping tools are specially developed...
A list is a container that contains different Python objects, which could be integers, words,...
What is Function in Python? A Function in Python is a piece of code which runs when it is...
Python allows you to quickly create zip/tar archives. Following command will zip entire directory...
What is PyTest? PyTest is a testing framework that allows users to write test codes using Python...
What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989....