Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/09-Empty-Section-Skip/05-Enumerate.ipynb
Views: 648
Kernel: Python 3
enumerate()
In this lecture we will learn about an extremely useful built-in function: enumerate(). Enumerate allows you to keep a count as you iterate through an object. It does this by returning a tuple in the form (count,element). The function itself is equivalent to:
Example
In [1]:
Out[1]:
0
a
1
b
2
c
enumerate() becomes particularly useful when you have a case where you need to have some sort of tracker. For example:
In [2]:
Out[2]:
a
b
enumerate() takes an optional "start" argument to override the default value of zero:
In [3]:
Out[3]:
[(3, 'March'), (4, 'April'), (5, 'May'), (6, 'June')]
Great! You should now have a good understanding of enumerate and its potential use cases.