last modified January 29, 2024
Python list loop shows how to iterate over lists in Python.
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 for statement iterates over the elements of a list in the order that they appear in the list.
#!/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.
#!/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
The enumerate function allows us to loop over list elements with
their indexes.
#!/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
The while statement is a control flow statement that allows code to be executed repeatedly based on a given boolean condition.
#!/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
A list comprehension is a syntactic construct which creates a list based on existing list.
#!/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
We can have nested lists inside another list.
#!/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
The zip function creates an iterator from the given iterables.
#!/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
Python datastructures - language reference
In this article we have looped over lists in Python.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.