Home Python Course Menu Book

Python Loops and Python Lists


Loops work very well for lists, as well as numbers. First, set up the following list in your code from the previous lesson:

superheroes = ["Jessica Jones", "Matt Murdock", "Kamala Khan", "Luke Cage"]

To loop through the items in this list, add a for loop:

for hero in superheroes:

Just like the range of numbers, the list you want to loop through goes on the end. The variable each item from the list will be stored in is called hero. Each time round the loop, Python will store a new value from the list into our hero variable.

Now add a print statement:


Your code should look like this (we've commented out the print line for the first loop):

A Python loop and a list

Run your code and the results in the output window are these:

Jessica Jones
Matt Murdock
Kamala Khan
Luke Cage

You can have a sorted list:

for hero in sorted( superheroes ):

To get a sorted list, just type the name of your list between the round brackets of the inbuilt sorted function. If you run the above code, you'd get this:

Jessica Jones
Kamala Khan
Luke Cage
Matt Murdock

The sort is on the first letter of the text in each list item. You can't use it sort something like firstname/surname.

Another thing to note is that the sort is temporary. If you add another loop below it:

for hero in superheroes:

The above code will print out the unsorted list.

Another inbuilt function useful with loops is enumerate. This function allows you to not only loop through your lists but to get at the index number as well. To see how it works, add the following line to your code:

nums = [1, 2, 3, 4, 5]

This just sets up a list of numbers.

Now set up a for loop:

for n in enumerate(nums):

The list you want to loop through goes between the round brackets of the enumerate function. Each number is then placed in the variable we've called n. Inside the loop, we're just printing out whatever is inside of n. Your code should look like this:

Using the enumerate function in a Python loop

Run the code and you'll see this printed out:

(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)

The first number in round brackets is the position of the item in the list. The second number is the actual value of the list item in that position.

You can get at the position number by adding another variable to the for line:

for pos, n in enumerate(nums):

Here, we've set up two variables before the in keyword, pos and n. The position number goes into the pos variable and the item at that position goes into the n variable.

Change your print line to this:

print("List position is:" + str(pos) + ", List item is:" + str(n))

Run your code again to see this in the output window:

List position is:0, List item is:1
List position is:1, List item is:2
List position is:2, List item is:3
List position is:3, List item is:4
List position is:4, List item is:5

If you want to loop through your list from the last item to the first, you can use the reversed function:

for hero in reversed(superheroes):

The printout from above would produce this:

Luke Cage
Kamala Khan
Matt Murdock
Jessica Jones

In the next lesson, you'll learn about the Python while loop. You'll also learn how to break out of loops.