In Python, the most direct way to iterate over the elements of a list is with a for loop. When you also need the position of each item, use enumerate(). A while loop or range(len(...)) is useful when your logic depends on list indexes.
Iterate over elements in a Python list
A Python list is an ordered collection, so you can visit its elements one after another. For most programs, iterate directly over the list instead of manually managing an index.
for element in my_list:
# use element
...
During each iteration, element refers to the next item in the list. This works for lists of numbers, strings, objects, dictionaries, and mixed data types.
1. Iterate over a Python list using a while loop
A while loop can iterate through a list by starting with index 0 and increasing the index until it reaches the length of the list. Use this form when you specifically need index-based control.
Python Program
aList = [2020, 'www\.tutorialKart.com', True]
index = 0
while index<len(aList):
print(aList[index])
index = index+1
Output
2020
www\.tutorialKart.com
True
The condition index < len(aList) keeps the index within the valid range. Since list indexes start at 0, the last element is at index len(aList) - 1.
Reference tutorials for the above program
2. Iterate over list elements using a for loop
A for loop is usually the simplest choice when you only need each value. Python assigns each list element to the loop variable in sequence.
Python Program
aList = [2020, 'www\.tutorialKart.com', True]
for element in aList:
print(element)
Otuput
2020
www\.tutorialKart.com
True
Here, the loop variable element receives each value directly, so no index variable is required. You can place any statements that should run once for every list item inside the loop body.
Reference tutorials for the above program
3. Iterate over a Python list with index using enumerate()
When you need both the index and the value, use enumerate(). It avoids manually incrementing a counter and keeps the loop easy to read.
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(index, color)
Output
0 red
1 green
2 blue
By default, enumerate() starts counting at 0. You can pass a second argument when you want a different starting number.
colors = ["red", "green", "blue"]
for position, color in enumerate(colors, start=1):
print(position, color)
Output
1 red
2 green
3 blue
4. Iterate through list indexes using range() and len()
You can also generate the valid indexes of a list with range(len(list)). This is useful when the index itself is part of the calculation or when you need indexed assignment.
numbers = [10, 20, 30]
for index in range(len(numbers)):
print(index, numbers[index])
Output
0 10
1 20
2 30
If you only need the values, prefer for element in list. If you need both index and value, enumerate() is generally clearer than range(len(...)).
5. Loop through a list of strings in Python
Iterating through a list of strings works the same way as iterating through any other list. Each loop iteration gives you one string.
names = ["Amit", "Neha", "Ravi"]
for name in names:
print(name.upper())
Output
AMIT
NEHA
RAVI
6. Iterate over a list of objects in Python
A list can contain instances of a class. During iteration, access the attributes or methods of each object through the loop variable.
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
products = [
Product("Mouse", 500),
Product("Keyboard", 1200)
]
for product in products:
print(product.name, product.price)
Output
Mouse 500
Keyboard 1200
7. Loop through a list of dictionaries in Python
For a list of dictionaries, each iteration returns one dictionary. You can then access its values using keys.
students = [
{"name": "Asha", "score": 82},
{"name": "Kiran", "score": 91}
]
for student in students:
print(student["name"], student["score"])
Output
Asha 82
Kiran 91
Modify list elements while iterating in Python
If you want to replace elements in the original list, assign through an index. Using enumerate() gives you both the index and the current value.
numbers = [1, 2, 3, 4]
for index, number in enumerate(numbers):
numbers[index] = number * 2
print(numbers)
Output
[2, 4, 6, 8]
For simple transformations, a list comprehension is often more concise and creates a new list instead of changing elements one by one.
numbers = [1, 2, 3, 4]
doubled = [number * 2 for number in numbers]
print(doubled)
Output
[2, 4, 6, 8]
Avoid changing list length during iteration
Removing or inserting elements in the same list while looping over it can cause items to be skipped because the indexes shift. If you need to remove selected values, iterate over a copy or build a new list.
numbers = [1, 2, 3, 4, 5]
for number in numbers.copy():
if number % 2 == 0:
numbers.remove(number)
print(numbers)
Output
[1, 3, 5]
Choose the right way to iterate over a Python list
| Requirement | Recommended approach |
|---|---|
| Use each list value | for element in list |
| Use both index and value | enumerate(list) |
| Work directly with indexes | range(len(list)) |
| Control iteration with an index condition | while loop |
| Create a transformed list | List comprehension |
| Change existing elements by position | enumerate() with indexed assignment |
Python list iteration summary
Use a for loop for straightforward list traversal. Use enumerate() when you need the index together with the element, and use index-based while or range(len(...)) loops when the index is central to the logic. When changing the size of a list, avoid mutating the same list directly during iteration.
In this Python Tutorial, we learned how to access or traverse through elements in a list.
TutorialKart.com