bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

What is Enumerate?

Before we jump into the understanding of enumerate function in Python programming language, let us unfold the chapter by finding out what enumerate is, what it means to enumerate something. The term enumerate is commonly used in mathematics and computer science. Well, enumerating is nothing but ordered listing of all the elements in a collection.

Now that we have an idea about what enumerate in general means, we can go ahead and start working on the enumerate function. Following is the list of all the topics that we will cover in this module, in case you need to jump to a specific one.

So, with further ado, let’s get started.

Enumerate Function

As mentioned earlier in order to perform enumerate Python programming language offers a simple function which not only makes iterating operations a lot easier but also helps our program look cleaner.

Let me give you prove this to you with the help of an example.

List = [“Red”, “Green”, “Black”, “Blue”]
for i in range(len(List)):
print(i, List[i])

Output:

0 Red
1 Green
2 Black
3 Blue

But the same thing can be achieved by one simple function without using range and length function, which can become tricky at times.
Let us see how enumerate makes it less complicated.

Enumerate a List

With the help of Python enumerate function we can iterate over the index and value in a Python list by using a basic for loop. Let us see how.

List = [“Red”, “Green”, “Black”, “Blue”]
for i, j in enumerate(List):
print(i, j)

Which gives the same output:

0 Red
1 Green
2 Black
3 Blue

Even though both the program has same length, you can tell just by looking that the second one with enumerate function is less complicated. One function is doing all the magic.
Fun fact: With the help of enumerate we can create a dictionary out of a list very easily. Let us see how:

List = [“Red”, “Green”, “Black”, “Blue”]
Dict=  dict(enumerate(List))
print(Dict)

Output:

{0: ‘Red’, 1: ‘Green’, 2: ‘Black’, 3: ‘Blue’}

Interesting right? Indeed. Well, not only in list, enumerate can be applied to other Python datatypes as well. Now let us go ahead see how to use enumerate in different datatypes in Python.
Data science masters program

Enumerate a Tuple

Just the way we performed enumerate in a list, we can do the same in a tuple. Let us try out one simple example.
Example:

List = [“Red”, “Green”, “Black”, “Blue”]
for i, j in enumerate(List):
print(i, j)

Output:

0 Red
1 Green
2 Black
3 Blue

Now that we have learnt how to enumerate a list and how to enumerate a tuple, let us see how to enumerate a list of tuples.

Enumerate a List of Tuples

List1 = [(10,”Red”), (5,”Green”), (8,”Black”),(2,”Blue”)]
for idx, (count, color) in enumerate(List1):
print(idx, count, color)

Output:

0 10 Red
1 5 Green
2 8 Black
3 2 Blue

This method of getting values out of tuple is also referred to as tuple unpacking.

Alright, let us move ahead and see how to apply enumerate over a string.

Enumerate a String

String = “Python”
for i, j in enumerate(String):
print(i, j)

Output:

0 P
1 y
2 t
3 h
4 o
5 n

Alright, as you might have noticed the enumerate by default starts from index 0, which is considered as the starting index. Now, with the help of enumerate function in Python, we can also specify the index that we want to operate the enumerate function from.

Enumerate from a specific index

Let us view how to enumerate from a specific index not from index 0 with the help of an example.

String = “Python”
for i, j in enumerate(String, 1):
print(i, j)

Output:

1 P
2 y
3 t
4 h
5 o
6 n

This often comes in handy when you don’t want to start the iteration from 0.

Enumerate inside a list comprehension

Using Python enumerate inside a list comprehension makes things very less complicated for programmer.
Example:

String = “Python”
for i, j in enumerate(String, 1):
print(i, j)

Output:

1 P
2 y
3 t
4 h
5 o
6 n

Now that we have learned how to use Python enumerate function in our programs in order to make the code less complicated go ahead to the next module learn another very interesting concept, Python Queue. See you there.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python Enumerate Function"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Enumerate Function.