You are currently viewing Python enumerate() Function With Example – Beginner’s Guide

Python enumerate() Function With Example – Beginner’s Guide

Introduction

First of all, What is the meaning of enumerating?

The meaning of enumerating is to number or name a list of things separately.

Python enumerate() function does the same thing as its name depicts itself.

Python enumerate()

Python enumerate() function takes a collection(e.g. a list or a tuple) and returns them as an enumerated object.

The enumerate() function adds a number/count to each item of the iterable object and returns an enumerate object.

Here’s a simple example of enumerate() function usage:

Output

Syntax

The syntax of enumerate() function is:

enumerate(iterable, start=0)

Parameters

iterable – must be a sequence, an iterator, or objects that supports iteration.

start – It is optional. If the start is not specified then the count will start from 0(default) else the count will start from a specified number and increment until the loop ends.

Output

We specified the start parameter which is equal to 10 and hence the enumerated output starts from the specified number.

Fact

If we see the above example, we converted our enumerate object into the list type and then print it.

What if we don’t convert our enumerated object into a list type then what will happen?

Well, the answer is pretty simple we don’t get any enumerated object instead, we’ll get the location of an enumerating object in the memory/CPU.

Example

Output

Looping over enumerate() object

We can perform for loop to iterate over enumerate objects.

Output

Performing enumerate() on

We are going to see the examples where we use the enumerate() function on different data types in Python.

String –

Python str type can be iterated so we can perform enumerate() function on it.

Example: Performing enumerate() on string

Output

We can use start parameter also –

Output

List –

We performed enumerate() function on list type in the example below where the first block of code gives the start value as 2 and we used a simple approach in the second block of code.

Example: Performing enumerate() on list

Output

Tuple –

Example: Performing enumerate() on tuple

Output

Dictionary –

dict() type contains key-value pairs, so we can enumerate() dictionary items (keys and values) separately.

Example: Performing enumerate() on dictionary

Output

Example: Performing enumerate() on dictionary using start

Output

Set –

set() type in Python is unordered, unchangeable, and unindexed, so when we enumerate them, the “set” item appears in random order.

Example: Performing enumerate() on set

Output

Conclusion

As we discussed above, Python enumerate() function helps us to add counter to our iterable objects.

You have seen the implementation of enumerate() function on the built-in data types in Python as well as you’ve seen how you can loop over iterable objects and enumerate them.

You can use this function in many ways like enumerating the files in the particular directory to make the folder structure pretty or you can make a tool where a user can automatically number their files and folders easily or maybe something else.


That’s all for now.

Keep Coding✌✌