Skip main navigation

Looping through a list

A 'for' statement can be used in Python to loop through a list. Watch Martin O'Hanlon explain how to use one.
2.8
You’re going to use iteration and lists to create a program which will create and display a shopping list. You’ll create this program in two parts, one displaying the shopping list and another creating the list. Now, your challenge will be to combine these two programs together. Now, the first step is to create a program which will loop through all the items in a list and print them to a screen.
28.3
So let’s create a new program and save it as display shopping.
39.9
Now we need to create a variable called shopping and add to it the items in our shopping list. So bread, cheese, apple, tomato, and biscuits.
72.2
Now, to loop through each of the items in this list, you will use a for loop.
79.4
For loops allow code to be run for a set number of times. In our case, it runs for the number of things that we have in our shopping list. Now, the syntax for a for loop looks like this.
94.8
We use for, and we say element in our list. Then we have our colon. Then we indent the code that we want to run as part of this loop in the same way that we did for a for loop. We say print, do this. Now, to use a for loop to print out each of the items in our shopping list, let’s change this program. So we’re going to say, for item in shopping, I’m going to say print the item. And if we run this program now, what we can see is the five items from our shopping list have been printed to the screen.
145.6
Now let’s look at how this works one line at a time by using the debugger.
155.3
So when we start the debugger, this is going to allow us to look at each one of these lines of code and see actually how it runs. So the first line of code, when we run it, it creates our shopping list. And actually, we can see that a variable is being created called shopping, which is a list of five items. And it starts our for loop. And when the first time it runs our for loop, we can see that a variable called item is created. And it’s got the value bread, being the first item in our shopping list. We then run the line of code to print that item, and we see bread appear on the screen.
198.2
And it then goes back to the for loop. And if we run our for loop again, we can see that the item is changed to cheese, the second item in our for loop. And again, if we step over that, it prints the item cheese to the screen. And this continues until there are no more items left in the shopping list, and the for loop exits.

You are going to use iteration and lists to create a program which will create and display a shopping list for you. You will create this program in two parts: one to displaying the shopping list, and another to create it. Your challenge will be to combine these two programs together.

We’ll start with the code to display the shopping items. The first step is to create a program which will loop through all the items in a list and print them to the screen.

  • Create a new program and save it as display_shopping.

Note: If you are unsure how to do this, take a look at the “If it’s this, then do that” step from Week 2.

  • Create a list variable for your shopping and fill it with some items.
shopping = ["bread", "cheese", "apple", "tomato", "biscuits"]

To loop through each of the items in the list, you will use a for loop. for loops exist in most programming languages, and their purpose is to allow code to be run a set number of times – in our case, equal to the number of things in our shopping list.

The syntax of a for loop in Python looks like this:

for element in list:
print("do this")
  • Use a for loop to print out each of the items in your shopping list.
for item in shopping:
print(item)

Note: The print statement is indented under the for loop. Indentation in a for loop works exactly the same as in an if statement; whatever code is indented under the for is run as part of that loop.

  • Run your program.

Mu IDE, showing code in the editor. The first line is setting up the list variable as above, while the second and third lines are the 'for item in shopping:' loop above. The REPL shows the five items from the shopping list, each on a new line.

Let’s break this down and consider what steps the computer takes to carry out this program.

  • First it creates the list called shopping.
shopping = ["bread", "cheese", "apple", "tomato", "biscuits"]
  • The for loop is entered. The first element in the shopping list, "bread", is put into the variable item.
for item in shopping:
  • print is used to display what is in the variable item.
 print(item)
  • item holds the value “bread”, so “bread” is printed.
bread
  • The for then runs again and the second element in the shopping list, "cheese" is put into the variable item.
for item in shopping:
  • print is used to display what is in the the variable item.
 print(item)
  • This time, item holds the value “cheese”, so “cheese” is printed.
cheese
  • The for loop runs again and the third element, "apple", is put into the variable item.
for item in shopping:
  • This continues until there are no more items in the list. Each item has been printed in turn, and then the for loop stops.
This article is from the free online

Programming 101: An Introduction to Python for Educators

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now