List is equivalent to arrays in other languages, with the extra benefit of being dynamic in size. In Python, list is a type of container in Data Structures, which is used to store multiple data at the same time. Unlike Sets, the list in Python are ordered and have a definite count.
There are multiple ways to iterate over a list in Python. Letâs see all different ways to iterate over a list in Python, and a performance comparison between them.
Method #1: Using For loop
# Python3 code to iterate over a list list = [1, 3, 5, 7, 9] # Using for loop for i in list: print(i) |
Output:
1 3 5 7 9
Method #2: For loop and range()
In case we want to use the traditional for loop which iterates from number x to number y.
# Python3 code to iterate over a list list = [1, 3, 5, 7, 9] # getting length of list length = len(list) # Iterating the index # same as 'for i in range(len(list))' for i in range(length): print(list[i]) |
Output:
1 3 5 7 9
Iteratiner the index is not recommended if we can iterate over the elements(as done in Method #1).
Method #3: Using while loop
# Python3 code to iterate over a list list = [1, 3, 5, 7, 9] # Getting length of list length = len(list) i = 0 # Iterating using while loop while i < length: print(list[i]) i += 1 |
Output:
1 3 5 7 9
Method #4: Using list comprehension (Possibly the most concrete way).
# Python3 code to iterate over a list list = [1, 3, 5, 7, 9] # Using list comprehension [print(i) for i in list] |
Output:
1 3 5 7 9
Method #5: Using enumerate()
If we want to convert the list into an iterable list of tuples (or get the index based on a condition check, for example in linear search you might need to save the index of minimum element), you can use the enumerate() function.
# Python3 code to iterate over a list list = [1, 3, 5, 7, 9] # Using enumerate() for i, val in enumerate(list): print (i, ",",val) |
Output:
0 , 1 1 , 3 2 , 5 3 , 7 4 , 9
Note: Even method #2 can be used to find the index, but method #1 canât (Unless an extra variable is incremented every iteration) and method #5 gives a concise representation of this indexing.
Method #6: Using Numpy
For very large n-dimensional lists (for example an image array), it is sometimes better to use an external library such as numpy.
# Python program for # iterating over array import numpy as geek # creating an array using # arrange method a = geek.arange(9) # shape array with 3 rows # and 4 columns a = a.reshape(3, 3) # iterating an array for x in geek.nditer(a): print(x) |
Output:
0 1 2 3 4 5 6 7 8
We can use np.ndenumerate() to mimic the behaviour of enumerate. The extra power of numpy comes from the fact that we can even control the way to visit the elements (Fortran order rather than C order, say :)) but the one caveat is that the np.nditer treats the array as read-only by default, so one must pass extra flags such as op_flags=[âreadwriteâ] for it to be able to modify elements.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python Iterate over multiple lists simultaneously
- Iterate over a dictionary in Python
- Iterate over a set in Python
- Iterate over characters of a string in Python
- Iterate over words of a String in Python
- Python - Iterate over Columns in NumPy
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- How to Iterate over Dataframe Groups in Python-Pandas?
- Different ways to iterate over rows in Pandas Dataframe
- How to iterate over rows in Pandas Dataframe
- How to iterate over a JavaScript object ?
- How to iterate over the keys and values with ng-repeat in AngularJS ?
- How to iterate over filtered (ng-repeat filter) collection of objects in AngularJS ?
- How to iterate over an Array using for loop in Golang?
- Java Program to Iterate Over Arrays Using for and foreach Loop
- Python | Ways to iterate tuple list of lists
- Python - Iterate through list without using the increment variable
- Python | Iterate through value lists dictionary
- Iterate associative array using foreach loop in PHP
- How to iterate through all selected elements into an array ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
