Understanding Enumerate function in python

Here in this post, I am going to talk about enumerate, a python function. Okay, here's a scenario: We have a=['a','b','c','d'] and we want to print all the elements in a along with their index. Now what we do is:

for i in xrange(len(a)):
     print i,a[i]
This can be done using enumerate as:
for i,item in enumerate(a):
     print i,item
These both have the results as:
0 a
1 b
2 c
3 d 
What if we want the index to start form 100? then we can do as:

for i,item in enumerate(a,start=100):
     print i,item
Now the output becomes 
100 a
101 b
102 c
103 d

You can check python documentation too.

Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud