How to Access Index using for Loop - Python
Last Updated :
30 Apr, 2025
When iterating through a list, string, or array in Python, it's often useful to access both the element and its index at the same time. Python offers several simple ways to achieve this within a for loop. In this article, we'll explore different methods to access indices while looping over a sequence:
Using range() and len()
One basic way to access the index while looping is by combining range() with len(). This allows you to manually retrieve elements by index.
Python
data = "GEEKFORGEEKS"
print("Ind and ele:")
for i in range(len(data)):
print(i, data[i])
OutputInd and ele:
0 G
1 E
2 E
3 K
4 F
5 O
6 R
7 G
8 E
9 E
10 K
11 S
Explanation: range(len(data)) generates index numbers. data[i] fetches the character at each index.
Using enumerate()
The enumerate() function returns both the index and the value during iteration, making the loop cleaner and more Pythonic.
Python
data = ["java", "python", "HTML", "PHP"]
print("Ind and ele:")
for i, val in enumerate(data):
print(i, val)
OutputInd and ele:
0 java
1 python
2 HTML
3 PHP
Explanation: enumerate(data) yields (index, value) pairs automatically. No need to manually calculate the index.
Using List Comprehension
List comprehension can also be used to access or generate indices and values separately in a compact way.
Python
data = ["java", "python", "HTML", "PHP"]
print("Indices:", [i for i in range(len(data))])
print("Elements:", [data[i] for i in range(len(data))])
OutputIndices: [0, 1, 2, 3]
Elements: ['java', 'python', 'HTML', 'PHP']
Explanation: [i for i in range(len(data))] creates a list of indices. [data[i] for i in range(len(data))] creates a list of values by index.
Using zip()
The zip() function can combine two lists: one with indices and one with elements, allowing simultaneous iteration.
Python
idx = [0, 1, 2, 3]
data = ["java", "python", "HTML", "PHP"]
print("Ind and ele:")
for i, val in zip(idx, data):
print(i, val)
OutputInd and ele:
0 java
1 python
2 HTML
3 PHP
Explanation: zip(idx, data) pairs each index with its corresponding element. Useful when you already have a list of indices.
Related articles:
Similar Reads
How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing
2 min read
Python | Pandas Index.to_frame() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.to_frame() function create a dataFrame from the given index with a column
2 min read
Python String index() Method The index() method in Python is used to find the position of a specified substring within a given string. It is similar to the find() method but raises a ValueError if the substring is not found, while find() returns -1. This can be helpful when we want to ensure that the substring exists in the str
2 min read
Python - Returning index of a sorted list We are given a list we need to return the index of a element in a sorted list. For example, we are having a list li = [1, 2, 4, 5, 6] we need to find the index of element 4 so that it should return the index which is 2 in this case.Using bisect_left from bisect modulebisect_left() function from bise
3 min read
Extracting rows using Pandas .iloc[] in Python Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. here we are learning how to Extract rows using Pandas .iloc[] in Python.Pandas .iloc[
7 min read
Access List Items in Python Accessing elements of a list is a common operation and can be done using different techniques. Below, we explore these methods in order of efficiency and their use cases. Indexing is the simplest and most direct way to access specific items in a list. Every item in a list has an index starting from
2 min read