How to Access Index in Python’s for Loop

21 Jun, 2022
In this article, we will discuss how to access index in python for loop in Python
Here, we will 4 different methods of Python program to access index of a list using for loop. we will cover different approaches to finding indexes in python for strings, lists, 
To handle looping requirements, the Python programming language supports the different types of loops. The loops can be executed in different ways in Python. FOR  Loops are one of them, and they’re used for sequential traversal. 
For instance, traversing a list, text, or array , i.e. for (i=0; in; i++). There is a “for in” loop, which is similar to other languages for each loop in Python. 
We can access the index in Python by using:
Using index elementUsing enumerate()Using List ComprehensionsUsing zip()
Using index element
Using enumerate()
Using List Comprehensions
Using zip()
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 subjectsdata = "GEEKFORGEEKS" print("Indices and Index value in the list:")  # display indices in the listfor 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
This method is used in for loop in Python get index of item in list which is used to get the index along with the corresponding element over the range using a List in Python.
Python3
# create a list of subjectsdata = ["java", "python", "HTML", "PHP"]  print("Indices and values in list:") # get the indices and values using enumerate methodfor i in enumerate(data):    print(i)
Output:
Indices and values in list:
(0, 'java')
(1, 'python')
(2, 'HTML')
(3, 'PHP')
This will make a list of the index and then gives the index and index values. 
Python3
# create a list of subjectsdata = ["java", "python", "HTML", "PHP"] print("Indices in list:") # get the indices  using list comprehension methodprint([i for i in range(len(data))]) print("values in list:") # get the values from indices  using list# comprehension methodprint([data[i] for i in range(len(data))])
Output:
Indices in list:
[0, 1, 2, 3]
values in list:
['java', 'python', 'HTML', 'PHP']
The zip method 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. below is the example for Python for loop index and value
Python3
# create a index list that stores listindexlist = [0, 1, 2, 3] # create a list of subjectsdata = ["java", "python", "HTML", "PHP"]  print("index and values in list:") # get the values from indices  using zip methodfor index, value in zip(indexlist, data):    print(index, value)
Output:
index and values in list:
0 java
1 python
2 HTML
3 PHP
In order to access the array of items refers to the index number. Use the index operator [ ] to access an item in an array. The index must be an integer.
Python3
# Python program to demonstrate# accessing of element from list # importing array moduleimport array as arr # array with int typea = arr.array('i', [1, 2, 3, 4, 5, 6]) # accessing element of arrayprint("Access element is: ", a[0]) # accessing element of arrayprint("Access element is: ", a[3]) # array with float typeb = arr.array('d', [2.5, 3.2, 3.3]) # accessing element of arrayprint("Access element is: ", b[1]) # accessing element of arrayprint("Access element is: ", b[2])
Output:
Access element is:  1
Access element is:  4
Access element is:  3.2
Access element is:  3.3
kapoorsagar226
surajkumarguptaintern
Picked
python-basics
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function