How to access index in for loop in Python
Hello programmers, we will see how to access index values in a for loop in Python in this tutorial.
Accessing index values and their corresponding values can be helpful in various cases. There are various ways to access the index and its values while using a loop in python.
Method 1 – Indexing using range() method in Python
Illustration of a function using range() method
def indexingList(dataList): print(f"Total length of the list is {len(dataList)}.") #total length of the list for i in range(len(dataList)): print(f"Index Value {i} <--> Element Value {dataList[i]}.") #printing each index value and data associated with it #driver if __name__ == "__main__": dataList = ["Element-0", "Element-1", "Element-2"] indexingList(dataList)
Output
Total length of the list is 3. Index Value 0 <--> Element Value Element-0. Index Value 1 <--> Element Value Element-1. Index Value 2 <--> Element Value Element-2.
Explanation
In the indexingList function, we are printing the total length of the list and using the for loop next, we are printing the index value as well as the element value associated with it.
Method 2 – Indexing using enumerate() method
Illustration of a function using enumerate() method
def indexingList(dataList): print(f"Total length of the list is {len(dataList)}.") #total length of the list print("Index and it's corresponding values are:") for i in enumerate(dataList): print(i) #printing the enumerate object #driver if __name__ == "__main__": dataList = ["Element-0", "Element-1", "Element-2"] indexingList(dataList)
Output
Total length of the list is 3. Index and it's corresponding values are: (0, 'Element-0') (1, 'Element-1') (2, 'Element-2')
Explanation
Enumerate() method returns an enumerating object with the first parameter as an iterable value and the second parameter being the value of the indexing counter position. In the following function, first, we print the total length of the list, and then using the enumerate() method, we print out the enumerating object iteratively using the for loop.
Method 3 – Indexing using zip() method
Illustration of a function using zip() method
def indexingList(indexList, dataList): print(f"Total length of the list is {len(indexList)}.") #length of the index list print(f"Total length of the list is {len(dataList)}.") #length of the data list for i,j in zip(indexList, dataList): print(f"Index Value {i} <--> Element Value {j}.") #printing each index and data associated with it #driver if __name__ == "__main__": indexList = [0, 1, 2] dataList = ["Element-0", "Element-1", "Element-2"] indexingList(indexList, dataList)
Output
Total length of the list is 3. Total length of the list is 3. Index Value 0 <--> Element Value Element-0. Index Value 1 <--> Element Value Element-1. Index Value 2 <--> Element Value Element-2.
Explanation
The zip() method is used to combine the index value list and the element value list. When using the zip() method, we use the two lists and combine them to produce the output. In this case, the function accepts two lists, one being the index and the other the element value list. After using the zip() method, the index and the elements are printed iteratively using for loop.
Conclusion
Indexes are structural data that speed up data access. Accessing the data elements associated with it becomes easy and faster which makes the whole process efficient. These are some of the ways of accessing the index in a for loop discussed above.
Leave a Reply