Enumerate() in Python | Python for Data Science | Day 12
Hello, Python tribe! We hope you are enjoying our content so far.
Today, we will be discussing a very easy topic – enumerate() in Python. So, without further ado, let’s quickly discuss what the enumerate() function does. Basically, enumerate() in Python is used to register the number of iterations, by returning a unique enumerate-type object.
Let’s code some examples to see how enumerate() functions.
Here, we have a list of IPL teams.
We create an enumerate() object called iplobj. You can also verify if an enumerate-type object is created using type(). Next, we convert the enumerate object i.e. iplobject into a list for printing it. The output is a s displayed above.
It implies that 0 corresponds to MI, 1 corresponds to RCb and so on. Thus, it enlists or enumerates the no. of iterations and displays them as a list of tuples.
Consider the code below:
Carefully notice that we have specified another parameter in enumerate(). Here, 1 denotes the starting point of iteration. The default value is 0, But, we have implicitly specified 1 here. So, the displayed list starts from 1 here.
Let’s try using enumerate() with a string.
Like the previous example, we have repeated the same steps but with a string.
You may also read about enumerate() here:
Now that you know about enumerate(), attempt the questions below without seeking help from the solutions we have provided.
Happy coding 🙂
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk