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:
You can check python documentation too.
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 aWhat if we want the index to start form 100? then we can do as:
1 b
2 c
3 d
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
Post a Comment