Python Lists Tutorial

When we make a shopping list, we don’t write each thing we need on a different piece of paper. We write it all on one. Why? Because this makes it easier to use!

When we make a program that works with many separate but similar items, it is possible to make a bunch of variables. However, it’s often far easier to make a list!

In this lesson, we’ll learn exactly how lists can make our lives easier.

Python Lists 1

Run the example:

Change the value of

to 1, then run it again.

Just like in the last lesson, the output is the same, but the code making that output is different. 


is set to 0, Python makes three variables, then prints them out.


is set to 1, Python makes one list, then uses a for loop to print all the items out.

Python Lists 2

After we have created a list, we can access any item in that list like this:


scary_things = ["clowns", "snakes"]

  • Question: Why did the above code print “snakes” and not “clowns”?
  • Answer: In Python, item 1 is not the first time. The first item is item 0. To print “clowns”:


scary_things = ["clowns", "snakes"]

The number we put in square brackets is called the index.

Python Lists 3

Changing the value of an item in a list is the same as changing the value of a variable.


scary_things = [“clowns”, “snakes”]
scary_things[1] = “bats”

scary_things = ["clowns", "snakes"]
scary_things[1] = "bats"
#Print the whole list this time

Add an item to the end of the list like this:


scary_things = ["clowns", "snakes"]

Python Lists 4

There are two ways to remove items from the list.

Remove by value:

scary_things = ["clowns", "snakes"]
# I'm not afraid of clowns anymore!

Remove by index:

scary_things = ["clowns", "snakes"]

We can use a function called

to get the number of items in that list.


scary_things = ["clowns", "snakes"]

Python Lists 5

Lists get even cooler when we combine them with for loops, as seen in the first example in this lesson.


scary_things = ["clowns", "snakes"]
for thing in scary_things:
print(thing, "are scary!")

  • Question: What is the value of
  • Answer: It changes each time the loop runs. On the first run, its value is “clowns.” On the second run, its value is “snakes.” The loop automatically ends when it’s done going through all the items in the list.

Python Lists 6

You can use the

keyword to test if something is in a list.


scary_things = ["clowns", "snakes"]
if "clowns" in scary_things:
print("I'm afraid of clowns.")

You can make lists of strings, ints, floats, and even other lists! A list holding other lists is essentially a 2D array.


floats_ints_strings = [
[1.5, 2.3, 6.4],
[6, 5, 4],
["mother", "father", "sister"]

The first index tells Python which list you want to access. The second index tells Python which item in that list you want to access.

Python Lists 7


Your annoying roommate mixed up all the food in the pantry and now you don’t know what belongs to whom. You decide to split up the items according to their expiration dates.

  • All even dates go to you.
  • All odd dates go to your roommate.
  • Things expiring before 2018 should probably be thrown away…

Make a program that sorts the food in the pantry , putting your items in one list, your roommate’s items in another list, and putting items that are too old in the trash. Don’t let any expired food mix in with the good food!

Python Lists 8

A possible answer:

food_dates_in_pantry = [
2018, 2019, 2019, 2020,
2019, 2022, 2025, 1932,
2020, 2017, 2018, 2012

trash = []
my_food = []
his_food = []

for date in food_dates_in_pantry:
if date < 2018:
elif date % 2 == 0 and date not in trash:
elif date % 2 != 0 and date not in trash:

print("My food:", my_food)
print("His food:", his_food)


For simplicity, have students use the same starting list. 

Since there are three expired items, their programs should keep 9 items.