Thursday, February 13, 2020

Today's Python Learning: Enumerate function in Python with example

Enumerate is a built-in function in python which adds a counter to an iterable (iterable are objects that can be used with loop e.g., lists, dictionaries, etc.). So, often, we set a separate counter variable in the loop; enumerate function can save us from that task. Let's look at the following example.



In the above example, we created a list of strings and passed it to the enumerate method. The enumerate method returns an enumerator object. For printing convenience, we converted it to a list and printed the tuples.

Then we did the tuple unpacking using a for loop to see the indexes.
The enumerate()  function also accepts the initial index setting, which is by default set to zero. So, the default start index is always zero.



Bonus  learning tips for Jupyter notebook:

Cells can be used with the shortcut in two ways -
1. Command Mode ( When the cell borders are blue)
2.Edit mode (When the cell border is green)
Clicking outside or inside the cells  can toggle between the edit modes

# If we want to insert a new cell below, we can use the shortcut button "B" in command mode
# Similarly double-pressing the "D" key twice will delete the current cell
# Shift+Enter(Return) will  execute the cell