Python : How to Iterate over a list ?

In this article we will discuss different ways to iterate over a list.

Iterate over the list using for-in loop

Its easies solution to iterate over the list i.e.

'''
 Iterate over the list using for loop
'''
for word in wordList:
    print(word)

Output
hi
hello
this
that
is
of

Iterate over the list using while loop

  • Fist get the size of list
  • Then iterate using while loop from 0 to len(list) – 1
  • In each iteration access iTh element

'''
 Iterate over the list using while loop
'''
i = 0
sizeofList = len(wordList) 
while i < sizeofList :
    print(wordList[i]) 
    i += 1

Output:
hi
hello
this
that
is
of

It has an advantage as compared to for-in loop. In each iteration, we know the index too. So, suppose if we want to iterate over the elements at odd number position only. Then with while loop we can do that i.e.
'''
 Iterate over the odd number position elements in list using while loop
'''
i = 0
sizeofList = len(wordList) 
while i < sizeofList :
    if i % 2 == 1 :
        print(wordList[i]) 
    i += 1

Output:
hello
that
of

Iterate over the list using for loop and range()

It too provides the access to index while iterating i.e.

'''
 Iterate over the list using for loop and range()
'''
for  i in range(len(wordList)) :
    print(wordList[i]) 
    i += 1

Output:
hi
hello
this
that
is
of

Advertisements

Iterate over the list using List Comprehension

Its a single line solution i.e.

'''
 Iterate over the list using List Comprehension
'''
[print(i) for i in wordList]

Output

 

hi
hello
this
that
is
of

Complete example is as follows,
def main():

    wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
    
    
    '''
     Iterate over the list using for loop
    '''
    for word in wordList:
        print(word)
        
    print("*************")    
    
    
    '''
     Iterate over the list using while loop
    '''
    i = 0
    sizeofList = len(wordList) 
    while i < sizeofList :
        print(wordList[i]) 
        i += 1
        
    print("*************")    
    
    '''
     Iterate over the odd number position elements in list using while loop
    '''
    i = 0
    sizeofList = len(wordList) 
    while i < sizeofList :
        if i % 2 == 1 :
            print(wordList[i]) 
        i += 1
        
    print("*************")    
    
    '''
     Iterate over the list using for loop and range()
    '''
    for  i in range(len(wordList)) :
        print(wordList[i]) 
        i += 1
        
    print("*************")    
    
    '''
     Iterate over the list using List Comprehension
    '''
    [print(i) for i in wordList]

if __name__ == '__main__':
    main()

Output:
hi
hello
this
that
is
of
*************
hi
hello
this
that
is
of
*************
hello
that
of
*************
hi
hello
this
that
is
of
*************
hi
hello
this
that
is
of

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

1 thought on “Python : How to Iterate over a list ?”

  1. Just a heads up…the example for
    Iterate over the list using for loop and range()
    should not include the i += 1 to actually iterate through the whole list as you can see in the output it produced…it is skipping values. You need to remove that to work like the other examples.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top