What is enumerate in Python

Any sequence in every programming language contains indices. An index is a value that indicates the position of a particular element in the sequence. We will encounter many situations in which we have to do the task with the help of these indices.

Then how can we find the location and corresponding element as well in a sequence? There is one method specially designed for this purpose in python which is “enumerate”. If we call this method upon any sequence, it returns index/location and the value/element present in that location. So whenever we call this enumerate function, it will return 2 values, one is an index of the element, and the other is the value of that element. Want to learn more about data science? Enroll in the Data Science institute in Chennai to do so.

Let’s find productivity of this function through the code:

list1=[‘a’,’ b’, ’c’, ’d’]

for loc,value in enumerate(list1):

print(loc,value)

What output will get is…

0 a

1 b

2 c

3 d

(0,1,2,3) are the locations/indices and (a,b,c,d) are the actual elements in those positions. In Python, the index starts from 0 always. Ok, is there any alternative to change my starting index..? Yes, you can. “enumerate” will do it for you. Earn yourself a promising career in data science by enrolling in the Data Science Classes in Hyderabad offered by 360DigiTMG.

 list1=[‘a’,’ b’, ’c’, ’d’]

for loc,value in enumerate(list1,start=1):

print(loc,value)

1 a

2 b

3 c

4 d

See, now the indices are (1,2,3,4) for the same elements. ‘start’ parameter of enumerate method will do this help for us.

We know, the set is a data type in Python which is unordered and not accessible. The position of elements in the set is not ordered, unlike lists/tuples. Looking forward to becoming a Data Scientist? Check out the Data Science Syllabus and get certified today.

Then how can we know which position contains which element? ‘enumerate’  is the easy option.

set1={‘a’,’ b’,’ c’,’ d’,’e’,’f’, ’i’}

for i,j in enumerate(set1,start=1):

print(i,j)

1 i

2 a

3 f

4 b

5 e

6 d

7 c

Look at the output, what you observed? The elements are randomly distributed. The locations are not the same at the time of their creation. We created  {a,b,c,d,e,f,i}  but now they are {i,a,f,b,e,d,c} . They are shuffled right. Also, check out these Data Science Course fees in Bangalore to start a career in Data Science.

What would be the case if applied on a dictionary as it contains key-value pairs?  If enumerate a dictionary, then we will get indices of” keys” and names of those keys 

d={‘a’:1, ’b’:2,’ c’:3}

for i,j in enumerate(d):

print(i,j)

0 a

1 b

2 c

(a,b,c) are the keys and (0,1,2) are their indices..

Finally, we got an idea about the enumerate() method in Python and its capabilities.

Become a Data Scientist with 360DigiTMG Data Science Course in Pune with placement. Get trained by the alumni from IIT, IIM, and ISB.

Data Science Training Institutes in Other Locations

Tirunelveli, Kothrud, Ahmedabad, Hebbal, Chengalpattu, Borivali, Udaipur, Trichur, Tiruchchirappalli, Srinagar, Ludhiana, Shimoga, Shimla, Siliguri, Rourkela, Roorkee, Pondicherry, Rajkot, Ranchi, Rohtak, Pimpri, Moradabad, Mohali, Meerut, Madurai, Kolhapur, Khammam, Jodhpur, Jamshedpur, Jammu, Jalandhar, Jabalpur, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Ernakulam, Erode, Durgapur, Dombivli, Dehradun, Cochin, Bhubaneswar, Bhopal, Anantapur, Anand, Amritsar, Agra , Kharadi, Calicut, Yelahanka, Salem, Thane, Andhra Pradesh, Greater Warangal, Kompally, Mumbai, Anna Nagar, ECIL, Guduvanchery, Kalaburagi, Porur, Chromepet, Kochi, Kolkata, Indore, Navi Mumbai, Raipur, Coimbatore, Bhilai, Dilsukhnagar, Thoraipakkam, Uppal, Vijayawada, Vizag, Gurgaon, Bangalore, Surat, Kanpur, Chennai, Aurangabad, Hoodi,Noida, Trichy, Mangalore, Mysore, Delhi NCR, Chandigarh, Guwahati, Guntur, Varanasi, Faridabad, Thiruvananthapuram, Nashik, Patna, Lucknow, Nagpur, Vadodara, Jaipur, Hyderabad, Pune, Kalyan.

Data Analyst Courses In Other Locations

Tirunelveli, Kothrud, Ahmedabad, Chengalpattu, Borivali, Udaipur, Trichur, Tiruchchirappalli, Srinagar, Ludhiana, Shimoga, Shimla, Siliguri, Rourkela, Roorkee, Pondicherry, Rohtak, Ranchi, Rajkot, Pimpri, Moradabad, Mohali, Meerut, Madurai, Kolhapur, Khammam, Jodhpur, Jamshedpur, Jammu, Jalandhar, Jabalpur, Gwalior, Gorakhpur, Ghaziabad, Gandhinagar, Erode, Ernakulam, Durgapur, Dombivli, Dehradun, Bhubaneswar, Cochin, Bhopal, Anantapur, Anand, Amritsar, Agra, Kharadi, Calicut, Yelahanka, Salem, Thane, Andhra Pradesh, Warangal, Kompally, Mumbai, Anna Nagar, Dilsukhnagar, ECIL, Chromepet, Thoraipakkam, Uppal, Bhilai, Guduvanchery, Indore, Kalaburagi, Kochi, Navi Mumbai, Porur, Raipur, Vijayawada, Vizag, Surat, Kanpur, Aurangabad, Trichy, Mangalore, Mysore, Chandigarh, Guwahati, Guntur, Varanasi, Faridabad, Thiruvananthapuram, Nashik, Patna, Lucknow, Nagpur, Vadodara, Jaipur, Hyderabad, Pune, Kalyan, Delhi, Kolkata, Noida, Chennai, Bangalore, Gurgaon, Coimbatore.

Navigate to:

360DigiTMG – Data Science, Data Scientist Course Training in Bangalore

Address: No 23, 2nd Floor, 9th Main Rd, 22nd Cross Rd, 7th Sector, HSR Layout, Bengaluru, Karnataka 560102
Phone no: 1800212654321

Get Directionhttps://g.page/360-digitmg-bangalore

Comments are closed.