Velamuvi AIAcademy

Lesson 16 of 30 · Collections

Lists

Store many values in one variable with Python lists. Create lists, read and change items by index, slice them and loop through them.

  • 8 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • What a list is and how to create one
  • How to read and change items using their index
  • How to take a slice of a list
  • How to check what a list contains and loop through it

One variable, many values

So far, each variable has held a single value. But real programs deal with collections: a class of students, a week of temperatures, a thousand product prices. Creating a separate variable for each one is impossible.

A list stores many values in order, under one name.

Try it yourselfPython
fruits = ["apple", "banana", "cherry"]
scores = [82, 95, 67, 74]

print(fruits)
print(scores)

A list is written with square brackets, with commas between the items. A list can hold any type of value, and it can be empty.

Try it yourselfPython
mixed = ["Ada", 36, True, 1.68]
empty = []

print(mixed)
print(empty)
print(len(mixed))

len() tells you how many items a list has.

Reading items by index

Each item has a position called an index. As with strings, counting starts at 0.

"apple""banana""cherry"
index 0index 1index 2
Try it yourselfPython
fruits = ["apple", "banana", "cherry"]

print(fruits[0])
print(fruits[1])
print(fruits[-1])

A negative index counts from the end, so fruits[-1] is always the last item, however long the list is.

Changing an item

Unlike strings, lists can be changed after they are created. Assign a new value to an index.

Try it yourselfPython
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"

print(fruits)

Adding an item

append() adds one item to the end of a list. You will use it constantly.

Try it yourselfPython
fruits = ["apple", "banana"]
fruits.append("cherry")
fruits.append("grape")

print(fruits)
print(len(fruits))

Slicing: taking part of a list

A slice returns a new list containing part of the original. scores[1:3] starts at index 1 and stops before index 3.

Try it yourselfPython
scores = [82, 95, 67, 74, 88]

print(scores[1:3])
print(scores[:2])
print(scores[2:])
print(scores[-2:])

Leaving out the first number means "from the start". Leaving out the second means "to the end".

Is it in the list?

The in keyword checks whether a value is present and gives True or False.

Try it yourselfPython
fruits = ["apple", "banana", "cherry"]

print("banana" in fruits)
print("kiwi" in fruits)

if "apple" in fruits:
    print("We have apples.")

Looping through a list

Lists and for loops belong together.

Try it yourselfPython
scores = [82, 95, 67, 74]
highest = scores[0]

for score in scores:
    if score > highest:
        highest = score

print("Highest score:", highest)

Building a list in a loop

Start with an empty list and append() to it. This pattern appears everywhere.

Try it yourselfPython
numbers = [1, 2, 3, 4, 5]
squares = []

for number in numbers:
    squares.append(number * number)

print(squares)

Common mistakes

Practice exercise

Change the second fruit to mango, add grape to the end, then print the list.

Your solutionPython
fruits = ["apple", "banana", "cherry"]

# 1. Replace "banana" with "mango"
# 2. Add "grape" to the end
# 3. Print the list. Output must be exactly:
# ['apple', 'mango', 'cherry', 'grape']
Knowledge check

If colours = ["red", "green", "blue"], what is colours[-1]?

Summary

  • A list stores many values in order: [1, 2, 3].
  • Indexes start at 0. Negative indexes count from the end.
  • Lists can be changed: items[0] = "new".
  • append() adds an item to the end, and len() counts the items.
  • in checks whether a value is in a list.

Next: more tools for working with lists.

Finished reading? Tick it off to track your progress.