![]() |
In this article, we will get to know how to access an index value in a for loop in Python. There are many ways to access an index value in a for loop in Python but we will be studying mainly four of them using a for loop. Python programming language supports the different types of loops, the loops can be executed in different ways. Access Index Value in a For Loop in PythonBelow are some of the examples by which we can access the index value in Python:
Python Access Index & Value Using range() FunctionIn this method, we are using the range() function to generate indices and access values in a list by their position. Python3
Output
Indices and Index value : Index: 0, Value: apple Index: 1, Value: banana Index: 2, Value: orange Access Index and Value Using enumerate() in PythonIn this method, we are using enumerate() in for loops to get the index value over the given range. Python3
Output
Indices and values in list: Index: 0, value: apple Index: 1, value: banana Index: 2, value: orange Python Access Index Value Using zip()The zip() method in Python is used to zip the index and values at a time, we have to pass two lists one list is of index elements and another list is of values. Python3
Output
index and values in list: 0 apple 1 banana 2 orange Access Index with Value Using itertools in PythonWe can also use count from itertools along with zip() to achieve a similar effect. In this example, we have used itertools to access the index value in a for loop. Python3
Output
Index: 0, Value: apple Index: 1, Value: banana Index: 2, Value: orange |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |