You can use the inbuilt method enumerate() available from Python version 2 to access the index in for loop in Python. Here is an example of getting the index id and value of a list in for loop in Python –
1 2 3 4 5 6 7 8 9 10 11 |
items = ["cat","dog","rat","ship","tiger"] for index, item in enumerate(items): print(index,item) ## Returns ## 0 cat ## 1 dog ## 2 rat ## 3 ship ## 4 tiger |