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 += 1

Explanation

  1. The variable my_list contains three items: 'apple', 'banana', and 'cherry'.
  2. A while loop runs as long as the counter i is less than the length of the list.
  3. Inside the loop, the element at index i is printed, and the counter is incremented by 1.
  4. This ensures all elements are printed in order from the list.

Output

apple
banana
cherry

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

  1. The variable my_list contains three items: 'apple', 'banana', and 'cherry'.
  2. The for loop directly iterates over each element in the list.
  3. The variable item holds each element of the list during each iteration and prints it.
  4. This approach is simple and effective when only the elements are required.

Output

apple
banana
cherry

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

  1. The variable my_list contains three items: 'apple', 'banana', and 'cherry'.
  2. The range(len(my_list)) generates a sequence of indices from 0 to the length of the list minus one.
  3. The for loop iterates over these indices, and the my_list[i] accesses each element by index.
  4. This approach is useful when both the index and elements are needed.

Output

apple
banana
cherry

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

  1. The variable my_list contains three items: 'apple', 'banana', and 'cherry'.
  2. The enumerate() function returns both the index and the item during each iteration.
  3. The for loop unpacks the index and item into variables index and item, respectively.
  4. The formatted string prints the index and the corresponding item in a readable format.

Output

mylist[0] = apple
mylist[1] = banana
mylist[2] = cherry

Summary

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.




Python Libraries