This is a detailed tutorial on the python enumerate() function. Learn to give number counts to different iterable objects like a list in python.
Python enumerate() Function
In python, enumerate() is a very useful function that can automatically assign sequenced numbering to different iterable objects. This function will return an Enumerate Type object, which can be easily iterated using Python List.
Syntax
The syntax of the enumerate() function is given below.
enumerateObject = enumerate(iterable, start = 0)
Arguments
It can take two arguments. The first one has to be an iterable object and the second one should be the numerical index that you want to assign to the first member of the provided iterable or the number from which it should start enumerating the iterable elements.
The second parameter, start
is optional and if you do not provide it as a parameter, the enumeration will start from index 0 as it is the default value of start
.
Return Type
The return type of this function is an enuerateObject
, which is also iterable and we’ve already discussed how you can easily traverse it using different loops or simple sequence data types such as a list.
The above screenshot shows that the return type of the function enumerate()
is an enumerate object.
Examples
The usage of the enumerate()
function is simple. The following examples give a quick look at how easily you can use this function to give sequenced numbering to different iterables.
Basic Usage of enumerate() Function
We’ve used this function in the example by providing a non-numbered list and a non-numbered tuple to convert them into a numbered list and tuple.
vegetables = ["Potato","Tomato","Peas","Cauliflower"] fruits = ("Mango","Apple","Papaya") #Non-Numbered List print(vegetables) #Non-Numbered tuple print(fruits) #Enumerating List vegetables = enumerate(vegetables) #Enumerating Tuple, setting starting index as 5 fruits = enumerate(fruits,5) #Converting Enumerable Object To Tuple vegetables = tuple(vegetables) #Converting Enumerable Object To List fruits = list(fruits) #Numbered vegetables Tuple print(vegetables) #Numbered fruits List print(fruits)
In the above code, I’ve defined a list and a tuple, both of which do not have any numbered indexes. Then I applied the function enumerate()
on both of these iterables, one without a start parameter and one with the start parameter as 5. As we know this function will return an enumerate type object.
So, as we can convert into any iterable, I converted the vegetables numbered iterable object into a list and fruits numbered iterable object to a numbered tuple. Therefore, the variable vegetables
which originally was a list is now a numbered tuple and the variable fruits which originally was a tuple is now a numbered list.
Doing iteration using enumerate()
You can also do numbered iterations using the enumerate() function easily. The following example illustrates the same.
cities = ["Ludhiana","Chandigarh","Amritsar","Jalandhar"] print("List of Cities") for city in cities: print(city) print("\nList of Cities with Numbering") for city in enumerate(cities): print(city) print("\nList of Cities with Ranks") for rank, city in enumerate(cities): print(rank, city) print("\nList of Cities With Rank Starting From 3") for rank, city in enumerate(cities, 3): print(rank, city)
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.