Skip to content

Beginner · Lists, tuples, dictionaries and sets · topic 13 of 50 · 20 min

Lists: many values in one name

Explanation

A list holds many values in order inside square brackets, like marks = [78, 91, 65]. Each item has a position number called an index, and Python starts counting at 0. You can change items, loop over them, check membership with in, and use len, sum, min and max.

One name for many values

So far each variable held one value. A class of 40 students would need 40 variables for their marks, which is slow and messy. A list solves this. A list is a collection: one name that holds many values in a fixed order. You write a list with square brackets [ ] and put commas between the items.

python
marks = [78, 91, 65, 88]
print(marks)
print(len(marks))
Shows the whole list, then 4. len() counts the items in a list.
Think of it like this: A list is like a train. Each coach carries one value, and the coaches always stay in the same order. The railway numbers its coaches, and Python numbers list items too, but Python starts counting at 0, so the first coach is coach 0.

Indexing: getting one item

The position number of an item is called its index. Write the list name, then the index in square brackets. The first item is at index 0, the second at index 1, and so on. A negative index counts from the end: -1 is the last item, -2 is the one before it.

python
fruits = ["mango", "banana", "guava"]
print(fruits[0])
print(fruits[2])
print(fruits[-1])
Shows mango, guava, guava. Here index 2 and index -1 both mean the last item.
Careful: The last index is len(list) - 1, not len(list). In a list of 3 items, fruits[3] gives IndexError: list index out of range. Beginners make this mistake very often.

Changing an item

Lists are mutable. Mutable means "can be changed after it is made". To change an item, assign a new value to its index.

python
prices = [40, 25, 60]
prices[1] = 30
print(prices)
Shows [40, 30, 60]. Only the item at index 1 changed.

Looping and checking with in

python
guests = ["Asha", "Ravi", "Meena"]
for name in guests:
    print("Welcome,", name)

print("Ravi" in guests)
print("John" in guests)
The for loop visits each item in order. in gives True or False: here True, then False.

sum, min and max

python
marks = [78, 91, 65, 88]
print(sum(marks))
print(min(marks), max(marks))
print(sum(marks) / len(marks))
Shows 322, then 65 91, then the average 80.5.

Mixed lists and the empty list

One list can hold different types together, like a name, an age and a True or False value. A list with nothing in it, [ ], is called an empty list. It is a common starting point: you begin with an empty list and add items later (the next lesson shows how).

python
student = ["Priya", 21, 8.5, True]
print(student[0], student[2])
cart = []
print(len(cart))
Shows Priya 8.5, then 0 because the empty list has no items.
Tip: Give lists plural names like marks, names or prices. Then a loop reads like English: for price in prices. Also remember that sum() only works on numbers, so do not call it on a list of names.

Watch the lesson

Lists: many values in one name · 1/7

Lists

In this lesson you will meet the list, a way to keep many values under one name.

Prefer a video by a teacher? Find free videos on “python lists tutorial for beginners indexing” on YouTube ↗

In real life: Checking a class's exam results

Mrs. Iyer has the maths marks of her class in one list. With a few lines she can find the class average, the highest mark, and how many students passed with 40 or more. If a new student joins, she adds one number to the list and the same code still works.

python
marks = [72, 45, 88, 31, 95, 60]
average = sum(marks) / len(marks)
passed = 0
for m in marks:
    if m >= 40:
        passed += 1
print("Average:", round(average, 1))
print("Highest:", max(marks))
print("Passed:", passed, "of", len(marks))
Average: 65.2, Highest: 95, Passed: 5 of 6.

Practice (0/3 done)

Task 1 · Code · Python

Write a function total_bill(prices) that returns the total of all the prices in the list. Example: total_bill([40, 25, 60]) returns 125.

Task 2 · Code · Python

Write a function count_passed(marks) that returns how many marks are 40 or more. Example: count_passed([72, 45, 88, 31]) returns 3.

Task 3 · Code · Python

Two lists go together: names[i] scored scores[i]. Write a function top_scorer(names, scores) that returns the name of the student with the highest score. If two students tie, return the one who comes first.

Quick quiz

Question 1

1.What does ["a", "b", "c"][1] give?

Question 2

2.A list has 5 items. Which index is the last one?

Question 3

3.What does print(len([10, 20, 30, 40])) show?

Question 4

4.After nums = [1, 2, 3] and nums[0] = 9, what is nums?

Question 5

5.What does "tea" in ["chai", "coffee"] give?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Common loop patterns