How can someone access the index itself for a list like the following :
intg = [8, 23, 45, 12, 78, 90]
When for is used to loop through, How can I access the loop index, from 1 to 6 in this case?
An additional state variable such as an index variable is considered non-pythonic which we normally use in languages like C or PHP.
We can use the built in function available in both Python 2 and 3 enumerate() it’s a better option
foridx, valin enumerate(intg):print(idx, val)
foridx, valin enumerate(intg):
There are some other methods too but this is the easiest and fastest one.
Hope this will help you.
It is very simple you can start it from 1 other than 0:
for index, item in enumerate(iterable, start=1): print index, item
for index, item in enumerate(iterable, start=1):
print index, item
You can use the following video tutorials to clear all your doubts:-
Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.
It's very simple to start it from 1 other than 0:
Certification in Digital Marketing |
Certification in Big Data Analytics |
Certification in Cloud & Devops |
Certification in Full Stack Web Development
Big Data and Data Science Master’s Course |
Data Science Architect Master’s Program |
Cloud and DevOps Architect Master’s Course |
Artificial Intelligence Engineer Master’s Course |
Microsoft Azure Certification Master’s Training
Data Science Course Online |
Big Data Hadoop Certification Training |
Machine Learning Course Online |
Artificial Intelligence Course and Training |
Salesforce Certification Training: Administrator and App Builder |
Tableau Training and Certification Course |
Python Data Science Course & Training |
Ethical Hacking Course |
Business Analyst Course |
Courses in Cyber Security
© COPYRIGHT 2011-2021 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.