Python List While Loop
To iterate over elements of a Python List using While Loop statement, start with index of zero and increment the index till the last element of the list using length of the list.
In this tutorial, we will go through example Python programs, that demonstrate how to iterate a list using while loop in Python.
Example 1 – Iterate Python List using While Loop
In this example, we will take a Python List, and iterate over all the elements of this list using while loop.
Python Program
list_1 = [4, 52, 6, 9, 21] index = 0 while index < len(list_1) : print(list_1[index]) index += 1Try Online
Output
4 52 6 9 21
Example 2 – Iterate Python List in Reverse using While Loop
In this example, we will take a Python List, and iterate over all the elements from end to start of this list using while loop.
Python Program
list_1 = [4, 52, 6, 9, 21] index = len(list_1) - 1 while index >= 0 : print(list_1[index]) index -= 1Try Online
Output
21 9 6 52 4
Example 3 – Iterate & Update Python List using While Loop
In this example, we will take a Python List on numbers, iterate over all the elements and increment each element by 2.
Python Program
list_1 = [4, 52, 6, 9, 21] #update list items index = 0 while index < len(list_1) : list_1[index] += 2 index += 1 #print list items index = 0 while index < len(list_1) : print(list_1[index]) index += 1Try Online
Output
6 54 8 11 23
Conclusion
In this Python Tutorial, we learned how to iterate over elements of a Python List using While Loop statement.