Beginner · Lists, tuples, dictionaries and sets · topic 13 of 50 · 20 min
Lists: many values in one name
Explanation
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.
marks = [78, 91, 65, 88]
print(marks)
print(len(marks))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.
fruits = ["mango", "banana", "guava"]
print(fruits[0])
print(fruits[2])
print(fruits[-1])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.
prices = [40, 25, 60]
prices[1] = 30
print(prices)Looping and checking with in
guests = ["Asha", "Ravi", "Meena"]
for name in guests:
print("Welcome,", name)
print("Ravi" in guests)
print("John" in guests)sum, min and max
marks = [78, 91, 65, 88]
print(sum(marks))
print(min(marks), max(marks))
print(sum(marks) / len(marks))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).
student = ["Priya", 21, 8.5, True]
print(student[0], student[2])
cart = []
print(len(cart))Watch the lesson
Lists: many values in one name · 1/7
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.
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))Practice (0/3 done)
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.
Write a function count_passed(marks) that returns how many marks are 40 or more. Example: count_passed([72, 45, 88, 31]) returns 3.
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
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.