Python - Loop Lists
Python - Loop Lists
Python provides various methods for looping through list items, with the most common
being the for loop. We can also use the while loop to iterate through list items, although
it requires additional handling of the loop control variable explicitly i.e. an index.
In a for loop, you can access each item in a sequence using a variable, allowing you to
perform operations or logic based on that item's value. We can loop through list items
using for loop by iterating over each item in the list.
Syntax
Following is the basic syntax to loop through items in a list using a for loop in Python −
Example
In the following example, we are using a for loop to iterate through each element in the
list "lst" and retrieving each element followed by a space on the same line −
Open Compiler
Output
https://www.tutorialspoint.com/python/python_loop_lists.htm 1/5
10/19/24, 9:34 PM Python - Loop Lists
25 12 10 -21 10 100
Learn Python in-depth with real-world projects through our Python certification
course. Enroll and become a certified expert to boost your career.
We can loop through list items using while loop by initializing an index variable, then
iterating through the list using the index variable and incrementing it until reaching the
end of the list.
An index variable is used within a loop to keep track of the current position or
index in a sequence, such as a list or array. It is generally initialized before the
loop and updated within the loop to iterate over the sequence.
Syntax
Following is the basic syntax for looping through items in a list using a while loop in
Python −
while condition:
# Code block to execute
Example
In the below example, we iterate through each item in the list "my_list" using a while loop.
We use an index variable "index" to access each item sequentially, incrementing it after
each iteration to move to the next item −
Open Compiler
my_list = [1, 2, 3, 4, 5]
index = 0
https://www.tutorialspoint.com/python/python_loop_lists.htm 2/5
10/19/24, 9:34 PM Python - Loop Lists
print(my_list[index])
index += 1
Output
1
2
3
4
5
We can loop through list items using index by iterating over a range of indices
corresponding to the length of the list and accessing each element using the index within
the loop.
Example
This example initializes a list "lst" with integers and creates a range of indices
corresponding to the length of the list. Then, it iterates over each index in the range and
prints the value at that index in the list "lst" −
Open Compiler
Output
lst[0]: 25
lst[1]: 12
lst[2]: 10
https://www.tutorialspoint.com/python/python_loop_lists.htm 3/5
10/19/24, 9:34 PM Python - Loop Lists
lst[3]: -21
lst[4]: 10
lst[5]: 100
We can iterate using list comprehension by specifying the expression and the iterable (like
a list, tuple, dictionary, string, or range). Following is the syntax −
This applies the expression to each item in the iterable and creates a list of results.
Example
In this example, we use list comprehension to iterate through each number in a list of
numbers, square each one, and store the squared result in the new list
"squared_numbers" −
Open Compiler
numbers = [1, 2, 3, 4, 5]
squared_numbers = [num ** 2 for num in numbers]
print (squared_numbers)
Output
We can iterate using the enumerate() function by applying it to the iterable. Following is
the syntax −
https://www.tutorialspoint.com/python/python_loop_lists.htm 4/5
10/19/24, 9:34 PM Python - Loop Lists
This provides both the index and item of each element in the iterable during iteration
Example
In the following example, we are using the enumerate() function to iterate through a list
"fruits" and retrieve each fruit along with its corresponding index −
Open Compiler
Output
0 apple
1 banana
2 cherry
https://www.tutorialspoint.com/python/python_loop_lists.htm 5/5