ZetCode

Python for loop

last modified January 29, 2024

Python list loop shows how to iterate over lists in Python.

Python loop definition

A loop is a sequence of instructions that is continually repeated until a certain condition is reached. For instance, we have a collection of items and we create a loop to go through all elements of the collection.

In Python, we can loop over list elements with for and while statements, and list comprehensions.

Python list loop with for

Python for statement iterates over the elements of a list in the order that they appear in the list.

list_loop_for.py
#!/usr/bin/python

words = ["cup", "star", "falcon", "cloud", "wood", "door"]

for word in words:
    
    print(word)

The example goes over the elements of a list of words with the for statement.

$ ./list_loop_for.py 
cup
star
falcon
cloud
wood
door

The for loop has an optional else statement which is executed when the looping has finished.

list_loop_for2.py
#!/usr/bin/python

words = ["cup", "star", "falcon", "cloud", "wood", "door"]

for word in words:

    print(word)
else:

    print("Finished looping")

We go over the list of words with a for loop. When the iteration is over, we print the "Finished looping" message which is located in the body following the else keyword.

$ ./list_loop_for2.py
cup
star
falcon
cloud
wood
door
Finished looping

Python list loop with enumerate

The enumerate function allows us to loop over list elements with their indexes.

list_loop_enumerate.py
#!/usr/bin/python

words = ["cup", "star", "falcon", "cloud", "wood", "door"]

for idx, word in enumerate(words):

    print(f"{idx}: {word}")

With the help of the enumerate function, we print the element of the list with its index.

$ ./list_loop_enumerate.py 
0: cup
1: star
2: falcon
3: cloud
4: wood
5: door

Python list loop with while

The while statement is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.

list_loop_while.py
#!/usr/bin/python

vals = [1, 2, 3, 4, 5, 6, 7]

n = len(vals)
i = 0
mysum = 0

while i < n:

    mysum += vals[i]
    i += 1

print(f'The sum is {mysum}')

The example calculate the sum of values in the list and prints it to the terminal.

$ ./list_loop_while.py
The sum is 28

Python list loop with list comprehension

A list comprehension is a syntactic construct which creates a list based on existing list.

list_compr.py
#!/usr/bin/python

words = ["cup", "star", "falcon", "cloud", "wood", "door"]

[print(len(word)) for word in words]

The example prints the length of each of the words in the list.

$ ./list_compr.py 
3
4
6
5
4
4

Python nested list loop

We can have nested lists inside another list.

loop_nested.py
#!/usr/bin/python

nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for i in nums:

    for e in i:

        print(e, end=' ')

    print()

We have a two-dimensional list of integers. We loop over the elements with two for loops.

$ ./loop_nested.py 
1 2 3 
4 5 6 
7 8 9 

Python list loop with zip

The zip function creates an iterator from the given iterables.

for_loop_zip.py
#!/usr/bin/python

words1 = ["cup", "bottle", "table", "rock", "apple"]
words2 = ["trousers", "nail", "head", "water", "pen"]

for w1, w2 in zip(words1, words2):

    print(w1, w2)

In the example, we iterate over two lists in one for loop.

$ ./for_loop_zip.py
cup trousers
bottle nail
table head
rock water
apple pen

Source

Python datastructures - language reference

In this article we have looped over lists in Python.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Python tutorials.