Python List - Loop through Items - For, While, Enumerate
Python - Loop through list
You can loop through a list in Python using While loop, or For loop. We will go through each of them and their variations with examples.
Examples
1. Loop through list using While loop
In the following example, we take a list my_list with three items. We have to iterate over this list using a Python While Loop.
Take a While loop with a counter i that increments in the loop over iterations from 0 up to the length of the list. Inside the loop, we access the item in the list using the counter as an index.
Python Program
my_list = ['apple', 'banana', 'cherry']
i = 0
while i < len(my_list):
print(my_list[i])
i += 1Explanation
- The variable
my_listcontains three items: 'apple', 'banana', and 'cherry'. - A
whileloop runs as long as the counteriis less than the length of the list. - Inside the loop, the element at index
iis printed, and the counter is incremented by 1. - This ensures all elements are printed in order from the list.
Output
apple
banana
cherry2. Loop through List using For loop
We can also use a Python For Loop to directly access the elements themselves rather than the index, inside the loop.
Python Program
my_list = ['apple', 'banana', 'cherry']
for item in my_list:
print(item)Explanation
- The variable
my_listcontains three items: 'apple', 'banana', and 'cherry'. - The
forloop directly iterates over each element in the list. - The variable
itemholds each element of the list during each iteration and prints it. - This approach is simple and effective when only the elements are required.
Output
apple
banana
cherry3. Loop through List using index in For loop
Using Python For Loop with range, we can loop through the index and access the item in the list using this index.
Python Program
my_list = ['apple', 'banana', 'cherry']
for i in range(len(my_list)):
print(my_list[i])Explanation
- The variable
my_listcontains three items: 'apple', 'banana', and 'cherry'. - The
range(len(my_list))generates a sequence of indices from 0 to the length of the list minus one. - The
forloop iterates over these indices, and themy_list[i]accesses each element by index. - This approach is useful when both the index and elements are needed.
Output
apple
banana
cherry4. Loop through List using enumerate() to access both the element and index inside the For loop
With enumerate() function, you can access both index and the item inside a For loop body.
Python Program
my_list = ['apple', 'banana', 'cherry']
for index, item in enumerate(my_list):
print(f"mylist[{index}] = {item}")Explanation
- The variable
my_listcontains three items: 'apple', 'banana', and 'cherry'. - The
enumerate()function returns both the index and the item during each iteration. - The
forloop unpacks the index and item into variablesindexanditem, respectively. - The formatted string prints the index and the corresponding item in a readable format.
Output
mylist[0] = apple
mylist[1] = banana
mylist[2] = cherrySummary
In this tutorial of Python Examples, we learned how to iterate through a list using looping statements, with the help of well-detailed example programs.