Enumerate functions

grocery = [‘Bread’, ‘milk’, ‘butter’]
enumerateGrocery = enumerate(grocery)
print(type(enumerateGrocery))

print(list(enumerateGrocery))
enumerateGrocery = enumerate(grocery, 20)
print(list(enumerateGrocery)

output:
<class ‘enumerate’>
[(0, ‘Bread’), (1, ‘milk’), (2, ‘butter’)]
[(20, ‘Bread’), (21, ‘milk’), (22, ‘butter’)]

please tell how and why the output come this and what is enumerate function and use of it?

Hey @shlok.sharma3, enumerate function is just used as a counter to maintain a count of iterations that has been made upto a certain point of time. So here since grocery is a list of 3 values, enumerate function by default starts from 0 and gives 0,1,2 to Bread, Milk and Butter respectively. In the other case, enumerate(grocery,20) , starts the counter from 20. So Bread gets 20 , and other two as 21 and 22. This enumerate function is very useful while using a for loop. I would strongly recommend you to read this post thoroughly to understand it in a more better way.

I hope this helps ! :+1:
Please mark the doubt as resolved in your doubts section ! :slightly_smiling_face:
Happy Learning ! :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.