Last modified: Jan 10, 2023 By Alexander Williams
Example: how to Loop Through a List in Python
in this article, we'll learn how to Iterate over a list with 2 different ways
Table Of Contents
1. Using for loop
Example:
#List
list_a = ['Mark', 'Jonh', 'Leo', 'Andrea']
#Iterate over array
for item in list_a:
print(item)
output
Mark Jonh Leo Andrea
2. Using while loop
Example:
#List
list_a = ['Mark', 'Jonh', 'Leo', 'Andrea']
i = 0
list_size = len(wordList)
while i < list_size :
print(list_a[i])
i += 1
output
Mark Jonh Leo Andrea
Summary
After knowing the ways to iterate over a list, Probably, you are asking your self, what is the best way to iterate over a list? the answer is the most common way to do that is by using for loop