Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Access Index in Python’s for Loop

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 31 Aug, 2022
Improve Article
Save Article

In this article, we will discuss how to access index in python for loop in Python.

Here, we will be using 4 different methods of accessing index of a list using for loop, including approaches to finding indexes in python for strings, lists, etc. Python programming language supports the different types of loops, the loops can be executed in different ways. FOR  Loops are one of them, and they’re used for sequential traversal. 

Access Index in For Loop With Examples

For an instance, traversing in a list, text, or array , there is a “for-in” loop, which is similar to other language’s “for-each” loop. 

We can access the index in Python by using:

  1. Using index element
  2. Using enumerate()
  3. Using List Comprehensions
  4. Using zip()

Using the index elements to access their values

The index element is used to represent the location of an element in a list. Here we are accessing the index through the list of elements. Here, we are using an iterator variable to iterate through a String.

Python3




# create a list of subjects
data = "GEEKFORGEEKS"
 
print("Indices and Index value in the list:")
 
 
# display indices in the list
for i in range(len(data)):
    print(i, data[i])


Output:

Indices and Index value in the list:
0 G
1 E
2 E
3 K
4 F
5 O
6 R
7 G
8 E
9 E
10 K
11 S

Using enumerate() method to access index

enumerate() is mostly used in for loops where it is used to get the index along with the corresponding element over the given range.

Python3




# create a list of subjects
data = ["java", "python", "HTML", "PHP"]
 
 
print("Indices and values in list:")
 
# get the indices and values using enumerate method
for i in enumerate(data):
    print(i)


Output:

Indices and values in list:
(0, 'java')
(1, 'python')
(2, 'HTML')
(3, 'PHP')

Using the list comprehension method to access index 

List comprehension will make a list of the index and then gives the index and index values. 

Python3




# create a list of subjects
data = ["java", "python", "HTML", "PHP"]
 
print("Indices in list:")
 
# get the indices  using list comprehension method
print([i for i in range(len(data))])
 
print("values in list:")
 
# get the values from indices  using list
# comprehension method
print([data[i] for i in range(len(data))])


Output:

Indices in list:
[0, 1, 2, 3]
values in list:
['java', 'python', 'HTML', 'PHP']

Using zip() method to access index 

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 elements. 

Python3




# create a index list that stores list
indexlist = [0, 1, 2, 3]
 
# create a list of subjects
data = ["java", "python", "HTML", "PHP"]
 
 
print("index and values in list:")
 
# get the values from indices  using zip method
for index, value in zip(indexlist, data):
    print(index, value)


Output:

index and values in list:
0 java
1 python
2 HTML
3 PHP

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!