4 Ways To Loop Through List With Index Using Python

Are you looking for ways to loop through List with Index using Python? In this article, I will show you four ways that you can use to Loop through a List with Index as you do in other programming languages such as JAVA, C++, and C.

In Python iterating through loops is really simple even if you do not know the index. But there can be scenarios when you are required to use the index of the particular element in the list or for all the elements in the list.

To get the index for all the elements in the list there are various methods available in Python that I am going to list down.

1. Using Enumerate Method to Loop Through List With Index

In Python Enumerate[1] method is present in the inbuilt functions library. This method returns a enumerate object that contains the index and element from the list. For example, if you have a list element at index 0 as 10 then enumerate object will be a tuple as (0, 10).

Now let us see in the below example code the usage of enumerate() method to loop through the list with index using Python.

#Python Code Example
#Initializing the List
givenList = ['You', 'Are', 'Visiting', 'Coduber']

#Using Enumerate to get the index 
#for above list
for index, words in enumerate(givenList):
    print("The Index for {} is {}".format(words, index))

Output:

The Index for You is 0
The Index for Are is 1     
The Index for Visiting is 2
The Index for Coduber is 3

As you can see from the above code I was able to get the index for each of the words present in the list using the enumerate method. As already discussed above it returns a tuple of index and element value at that index from the list.

2. Naive Approach To Loop Through List With Index

This approach can be used when you want to loop through the list in Python using the while method. As if you are using the while approach you can have only one variable to track the index in the list and then print the list using the index value.

Let us see in the below example code to loop through the list with index using While Loop.

#Python Code Example
#Initializing the List
givenList = ['You', 'Are', 'Visiting', 'Coduber']

#Getting Index using while Loop
index = 0
while index<len(givenList):
    print(index, givenList[index])
    index += 1

Output:

0 You
1 Are
2 Visiting
3 Coduber

3. Using Range Method To Loop Through List With Index

You can also use the range method present in Python. You can initialize a variable as an index of 0 and then loop till the range of the list and get the index from the variable and element value from the list with the index value.

Let us see in the below example code the usage of the range method in Python to Loop through List.

#Python Code Example
#Initializing the List
givenList = ['You', 'Are', 'Visiting', 'Coduber']

#Getting Index using while Loop
for index in range(0, len(givenList)):
    print(index, givenList[index])

Output:

0 You
1 Are
2 Visiting
3 Coduber

As you can see in the above code, using For loop till the range of 0 to the end of the length of the list I was able to get the index value and print it with the element as well.

4. Using Zip Method To Loop Through List With Index

Another method or function in Python that you can use to loop through and get the index is zip(). But this method does not provide any specific advantages from the above three methods mentioned.

Let us see in the below example code the usage of zip() function to get the index of the list using Python.

#Python Code Example
#Initializing the List
givenList = ['You', 'Are', 'Visiting', 'Coduber']

#Getting Index using while Loop
for indexAndValue in zip(range(len(givenList)), givenList):
    print(indexAndValue)

Output:

(0, 'You')
(1, 'Are')
(2, 'Visiting')
(3, 'Coduber')

As you can see in the above code, all the zip method does is combine the range function output and each element from the list into a tuple. And it returns a tuple of the index and its value from the list.

4 Ways To Loop Through List With Index Using Python

Wrap Up

I hope you learned how to loop through a list with an index in Python. I listed out four methods that you can use. The most widely used method from the above list is the enumerate one. It is the fastest and easiest method to use.

Let me know in the comment section if you have any better methods than the ones discussed above I will be happy to add those here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Code Examples: Multiply In Python
  2. How To Join Words In a List Using Python Code Example
  3. Python Code Example: How To Find Element in a List in 6 Way
  4. How To Test For An Empty Object In JavaScript
  5. How To Iterate Over Rows in a DataFrame in Pandas Python
  6. Python Get Filename From A Path Without Extension 4 Ways
  7. How To Concatenate String and Integer In Python

Leave a Comment