Enumerate() Function in Python
In programming, we often have to deal with large volumes of data and return the count of our data. Knowing how to count elements in Python is a basic and crucial skill.
Additionally, as a programmer, your code must be clean, straightforward, and readable by someone other than you. Here, we will explore a function in Python that enables you to count objects in your lists, sets, tuples, and dictionaries in an uncomplicated manner.
What is the enumerate() function?
The enumerate() function in Python returns the ordered listing of objects in a group. In simpler terms, it allows you to count objects in a collection. The enumerate function works by iterating through each item in a collection and returning the indexes of each object in that group.
enumerate() syntax
[python]enumerate(iterable, start=0)[/python]enumerate() function first accepts the iterable and specifies what number the counter should start from. By default, your counter will start from the index of zero.
The enumerate() function is mostly used in for loops. However, you can use it directly by creating enumerated objects. See the example below:
[python]random_list = ["swing", "jump", "run", "crawl"]enumerate_object = enumerate(random_list)print (list(enumerate_object)[/python]
Enumerating a string with the enumerate function.
The code above first creates a list of strings, then enumerates the strings using the enumerate() function. Finally, we convert our enumerated variable to a list of tuples with their indexes.
Now that we understand the syntax of enumerate(), let’s see examples using enumerate in for loops for different data structures.
Using the enumerate() function with sets
A set is an immutable, unordered data type used to store data in Python. Its unordered quality means you cannot index a set, making it difficult to target items in a set. However, the enumerate function solves this problem by assigning a counter to each object in the set. Let’s see an example below:
[python]set_of_objects = {"calipers", "dividers", "compasses"}for index, objects in enumerate(set_of_objects, start=1):print(index, objects)[/python]
Our for loop means that we want our code to loop through each object in the set_of_objects variable and assign an index starting from one to each object. In our last line of code, we print our index and the objects.
The result:
We looped through each item in the set and added an index to each of them.
Using the Enumerate Function with Lists
When working with lists, you may need to return the index of each element in your list. Contrary to a set, lists are indexable. Therefore, by declaring a count variable and incrementing the value of count for each object with a for loop, we can return the objects in a list and their index.
See the example below:
[python]random_list = ["chicken", "potato", "ice-cream"]count = 0for food in random_list:
count += 1print(count, food)[/python]
The result:
Looping through our list and incrementing the number of count for each object.
Get CodingCampus’s stories in your inbox
Join Medium for free to get updates from this writer.
The problem with the count function is that you declare the count variable outside the for loop and the scope where you implement the counting. However, the enumerate() function allows you to perform the same indexing with fewer lines of code.
Let’s see an example with the enumerate() function below:
[python]random_list = ["chicken", "potato", "ice-cream"]for number, food in enumerate(random_list, start=1):print(number, food)[/python]
The result:
With the enumerate function, we indexed our list with a fewer lines of code.
Using the Enumerate Function with Dictionaries
To get our key and value pair, we will first use the items() method to convert the dictionary to a list.
[python]fruit_pair = {"A": "apple", "B": "banana", "C": "cherries", "D": "dragonfruit"}for index, fruit in enumerate(fruit_pair.items(), start=1):print (index, fruit)[/python]
The result:
Indexing a dictionary with the enumerate function and the items method.
The items() function is important for this step; otherwise, our code will print only the key of the dictionary and the index of that key. For example:
[python]fruit_pair = {"A": "apple", "B": "banana", "C": "cherries", "D": "dragonfruit"}for index, fruit in enumerate(fruit_pair, start=1):print (index, fruit)[/python]
The result:
Indexing a dictionary with the enumerate function and without the items function.
Using the Enumerate Function with Tuples
Tuples are also ordered, and using the enumerate function with a tuple is similar to using it with a list. For example:
[python]random_tuple = ("Louise", "Clair", "Sam", "Chad")for index, name in enumerate(random_tuple, start=1):print(index, name)[/python]
The result:
Indexing a tuple with enumerate function
When to Use the Enumerate Function
The enumerate() function is one of the many functions that optimize your code. It is memory-efficient, clean, and concise. If you need to write the indexes of objects of any data type concisely, the enumerate() function is the best method.









