How to access index and value in for loop – Python?
This tutorial guides you on how to access index and value in for loop In Python using built-in functions like enumerate(), range() functions and also using while loop.
Access index and value in for loop – Python
There are multiple ways using which you can access loop index in Python. The following are some examples.
using enumerate()
The enumerate built-in option is the best approach to access index and value in for loops. This option is available in both Python 2 and 3.
Here is a sample program, which enumerates the list of integers and prints the index and value of each item in the list in for loop.
For example,
Β
numbers = [4, 6, 10, 24, 28, 30] for indx, val in enumerate (numbers): print('index is {}'.format(indx), ', value is {}'.format(val))
Output
index is 0 , value is 4 index is 1 , value is 6 index is 2 , value is 10 index is 3 , value is 24 index is 4 , value is 28 index is 5 , value is 30
Another approach using for loop
In this approach, you would use Python’s for loop itself to find the loop index as shown below.
numbers = [4, 6, 10, 24, 28, 30] indx=0 for item in numbers: print('index is {}'.format(indx), ', value is {}'.format(item)) indx = indx + 1
Output
index is 0 , value is 4 index is 1 , value is 6 index is 2 , value is 10 index is 3 , value is 24 index is 4 , value is 28 index is 5 , value is 30
range() function in for loop
There is another way that you can access loop index by using range function in for loop as shown in the example below.
For example,
for indx in range(len(numbers)): print('index is {}'.format(indx), ', value is {}'.format(numbers[indx]))
Output
index is 0 , value is 4 index is 1 , value is 6 index is 2 , value is 10 index is 3 , value is 24 index is 4 , value is 28 index is 5 , value is 30
while loop to access loop index
Also, you can use while loop to access the loop index in the following way.
For example,
indx = 0 while indx < len(numbers): print('index is {}'.format(indx), ', value is {}'.format(numbers[indx])) indx = indx + 1
Output
index is 0 , value is 4 index is 1 , value is 6 index is 2 , value is 10 index is 3 , value is 24 index is 4 , value is 28 index is 5 , value is 30
That’s it. You had learnt how to access index and value in for loop using enumerate(), range() functions and also using while loop.
Hope it helped π
You’ll also like:
- String Formatting in Python β Examples
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Create list of lists in Python
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns β Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Python program to find the greatest of three numbers
- Find difference between two given numbers in Python
- String indexing in Python with Examples
- Embed HTML within IPython Notebook
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not βstrβ β Python3
- Extract numbers from a string in python
- Java String substring() example program
- Python Lists with Examples