How to Access Index using for Loop - Python Last Updated : 30 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ComprehensionList 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:map()zip()list comprehension Comment More infoAdvertise with us Next Article How to Access Index using for Loop - Python gottumukkalabobby Follow Improve Article Tags : Python python-basics Practice Tags : python 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 Pandas Extracting rows using .loc[] - Python Pandas provide a unique method to retrieve rows from a Data frame. DataFrame.loc[] method is a method that takes only index labels and returns row or dataframe if the index label exists in the caller data frame. To download the CSV used in code, click here.Example: Extracting single Row In this exam 3 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 Python Foreach - How to Implement ? Foreach loop is a convenient way to iterate over elements in a collection, such as an array or list. Python, however, does not have a dedicated foreach loop like some other languages (e.g., PHP or JavaScript). We will explore different ways to implement a foreach-like loop in Python, along with exam 3 min read Use for Loop That Loops Over a Sequence in Python In this article, we are going to discuss how for loop is used to iterate over a sequence in Python. Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for loop we can iterate a sequence of elements 3 min read How to Replace Values in a List in Python? Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using 2 min read Like