Access the Index and Value using Python 'For' Loop
Last Updated :
06 Feb, 2024
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 Python
Below are some of the examples by which we can access the index value in Python:
- Using range() function
- Using enumerate() function
- Using zip() function
- Using itertools
Python Access Index & Value Using range() Function
In this method, we are using the range() function to generate indices and access values in a list by their position.
Python3
# create a list of fruits
fruits = ['apple', 'banana', 'orange']
print("Indices and Index value :")
# Iterate over the indices of the list and access elements using indices
for i in range(len(fruits)):
print(f"Index: {i}, Value: {fruits[i]}")
OutputIndices and Index value :
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: orange
Access Index and Value Using enumerate() in Python
In this method, we are using enumerate() in for loops to get the index value over the given range.
Python3
# Method 2: Using enumerate
fruits = ['apple', 'banana', 'orange']
print("Indices and values in list:")
# Use enumerate to get both index and value in the loop
for i,fruit in enumerate(fruits):
print(f"Index: {i}, value: {fruit}")
OutputIndices 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
# Method 3: Using zip
# create a index list that stores list
indexlist = [0, 1, 2, 3]
# create a list of fruits
fruits = ['apple', 'banana', 'orange']
print("index and values in list:")
# get the values from indices using zip method
for index, value in zip(indexlist, fruits):
print(index, value)
Outputindex and values in list:
0 apple
1 banana
2 orange
Access Index with Value Using itertools in Python
We 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
from itertools import count
# Sample list
fruits = ['apple', 'banana', 'orange']
# Create an iterator that produces consecutive integers starting from 0
index_counter = count()
# Use zip to combine the index_counter with the list elements
for i, fruit in zip(index_counter, fruits):
print(f"Index: {i}, Value: {fruit}")
OutputIndex: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: orange
Similar Reads
How to Access Dictionary Values in Python Using For Loop A dictionary is a built-in data type in Python designed to store key-value pairs of data. The most common method to access values in Python is through the use of a for loop. This article explores various approaches to accessing values in a dictionary using a for loop. Access Dictionary Values in Pyt
2 min read
Accessing index and value in Python list We are given a list, and our task is to access both the index and value of each element in the list using Python. For example, using enumerate(list) in a loop like for index, value in enumerate(list) allows us to access both the index and the value together.Using enumerate() enumerate() is preferred
2 min read
How to Store Values in Dictionary in Python Using For Loop In this article, we will explore the process of storing values in a dictionary in Python using a for loop. As we know, combining dictionaries with for loops is a potent technique in Python, allowing iteration over keys, values, or both. This discussion delves into the fundamentals of Python dictiona
3 min read
Get Values from Dictionary Using For Loop - Python In Python, dictionaries store data as key-value pairs and we are given a dictionary. Our task is to extract its values using a for loop, this approach allows us to iterate through the dictionary efficiently and retrieve only the values. For example, given d = {'a': 1, 'b': 2, 'c': 3}, we should extr
2 min read
Find the Index of a Substring in Python Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.Using str.find() The find() method searches for the first occurrence of the specified substring and return
3 min read
How to Find Index of a Substring in Python In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string.Using find() methodThe
2 min read