What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

6 Ways to Iterate through a List in Python (with Code)

  • Mar 15, 2021
  • 7 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
6 Ways to Iterate through a List in Python (with Code)

In this article, we will study what is a list in python language and different ways to iterate through a list in python programming along with the python code for the same. So, let’s start!

What is a List in Python?

List in python language is used to store multiple elements in a single variable. Lists are one of the 4 data types in python language along with tuple, set, and dictionary. List in Python is created by putting elements inside the square brackets, separated by commas. A list can have any number of elements and it can be of multiple data types. Also, all the operation of the string is similarly applied on list data type such as slicing, concatenation, etc. Also, we can create the nested list i.e list containing another list.

6 Ways to Iterate through a List in Python

There are multiple ways through which we can iterate the list in python programming. Let us study them one by one below:

Using for loop

The easiest method to iterate the list in python programming is by using them for a loop. The method of the iterating list using for loop is as given below

list = [1, 2, 3, 4, 5]

# Iterating list using for loop
for i in list:
    print(i)

The output of the above code is as given below:

 1

 2

 3

 4

 5

Using loop and range() function

Another method to iterate the list while programming in python is by using the loop and range() function together. Iterating the list using this method is not recommended if iteration using for loop(method 1) is possible. The method to do so is as given below:

list = [1, 2, 3, 4, 5]
# getting length of list using len() function
length = len(list)

# using for loop along with range() function
for i in range(length):
    print(list[i])

The output of the above code is as given below:

 1

 2

 3

 4

 5

Using While loop

We can also iterate the list in python language using a while loop. The method to use while loop for the iterating list is as given below:

list = [1, 2, 3, 4, 5]

# Getting length of list using len() function
length = len(list)
i = 0

while i < length:
    print(list[i])
    i += 1

The output of the above code is as given below:

 1

 2

 3

 4

 5

Using list comprehension

This is the most concrete way to iterate the list while programming in the python language. The method to iterate list using list comprehension is as given below:

list = [1, 2, 3, 4, 5]

# Iterating list using list comprehension
[print(i) for i in list]

The output of the above code is as given below:

 1

 2

 3

 4

 5

Using enumerate() function

There are few times when you may need the display the index of the element along with the element in the list itself. In such cases, we use the enumerate() function for the iteration of the list. The method to use the enumerate() function to iterate the list is as given below:

list = [1, 3, 5, 7, 9]

# Using enumerate() function to iterate the list
for i, val in enumerate(list):
    print (i, ",",val)

The output of the above code is as given below:

 0, 1

 1, 3

 2, 5

 3, 7

 4, 9

Using Numpy function

All the methods that we discussed till now are generally preferable for small or say single-dimensional lists. But when it comes to large n-dimensional lists it is advisable to use an external library such as NumPy for iterating lists. The method to use the Numpy function for iterating lists is as given below:

# Importing external library
import numpy as np

a = np.arange(5)

for x in np.nditer(a):
    print(x)

The output of the above code is as given below:

 0,

 1

 2

 3

 4

Conclusion

Therefore, in this article, we studied what is the list in python programming and different methods to iterate the list while programming in python. Also, we learned the code to use that method by an example and its respective output.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.