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 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


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 list using for loop and range()

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


Iterate over the list using List Comprehension

Its a single line solution i.e.



Complete example is as follows,



Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.