Python Lists
Learn how to create lists, access items by index, add and remove elements, and use the len() function.
What Are Lists?
A list is a Python data structure that stores multiple items in a single variable. Think of it as a numbered collection -- like a shelf where each slot has a position number. Lists are created using square brackets [], and items are separated by commas.
# Creating lists
fruits = ["apple", "banana", "cherry"]
scores = [85, 92, 78, 90]
mixed = ["Ava", 15, True, 3.14] # Lists can hold different types
# An empty list
empty = []
print(fruits) # Output: ['apple', 'banana', 'cherry']
print(len(scores))# Output: 4 (number of items)
Lists are one of the most commonly used data structures in Python. They are ordered (items stay in the order you add them), mutable (you can change them after creation), and allow duplicate values.
Indexing and Accessing Items
Each item in a list has an index (position number). Python uses zero-based indexing -- the first item is at index 0, the second at index 1, and so on. Negative indices count from the end.
colours = ["red", "green", "blue", "yellow"]
# 0 1 2 3
# -4 -3 -2 -1
print(colours[0]) # Output: red (first item)
print(colours[2]) # Output: blue (third item)
print(colours[-1]) # Output: yellow (last item)
print(colours[-2]) # Output: blue (second-last)
# Changing an item
colours[1] = "purple"
print(colours) # Output: ['red', 'purple', 'blue', 'yellow']
If you try to access an index that does not exist (e.g. colours[10]), Python raises an IndexError. Always check your index is within range!
Adding and Removing Items
Lists are mutable -- you can add, remove, and modify items after creation. Python provides several built-in methods:
animals = ["cat", "dog"]
# Adding items
animals.append("rabbit") # Add to end
print(animals) # ['cat', 'dog', 'rabbit']
animals.insert(1, "bird") # Insert at index 1
print(animals) # ['cat', 'bird', 'dog', 'rabbit']
# Removing items
animals.remove("dog") # Remove by value
print(animals) # ['cat', 'bird', 'rabbit']
last = animals.pop() # Remove and return last item
print(last) # rabbit
print(animals) # ['cat', 'bird']
# Useful functions
numbers = [5, 2, 8, 1, 9]
print(len(numbers)) # 5 (length)
print(min(numbers)) # 1 (smallest)
print(max(numbers)) # 9 (largest)
print(sum(numbers)) # 25 (total)
numbers.sort()
print(numbers) # [1, 2, 5, 8, 9] (sorted)
Key Vocabulary
List
An ordered, mutable collection of items defined with square brackets, e.g. [1, 2, 3].
Index
The position number of an item in a list. Python uses zero-based indexing (first item is index 0).
append()
A list method that adds an item to the end of the list.
len()
A built-in function that returns the number of items in a list (or other collection).
Worked Examples
Create a list of test scores and calculate the average.
scores = [85, 92, 78, 90, 88]
average = sum(scores) / len(scores)
print(f"Average score: {average}") # Output: Average score: 86.6
Step 1: Use sum() to add all scores together.
Step 2: Use len() to count how many scores there are.
Step 3: Divide the total by the count to get the average.
Build a shopping list by asking the user for items.
shopping = []
for i in range(3):
item = input("Enter an item: ")
shopping.append(item)
print(f"\nYour shopping list ({len(shopping)} items):")
for item in shopping:
print(f" - {item}")
Step 1: Start with an empty list.
Step 2: Use a loop to ask for 3 items, appending each to the list.
Step 3: Loop through the list to display all items.
Find and display the highest and lowest scores.
scores = [72, 95, 63, 88, 91, 55]
highest = max(scores)
lowest = min(scores)
print(f"Highest: {highest}") # Output: Highest: 95
print(f"Lowest: {lowest}") # Output: Lowest: 55
print(f"Range: {highest - lowest}") # Output: Range: 40
Step 1: Use max() to find the largest value.
Step 2: Use min() to find the smallest value.
Step 3: Calculate the range by subtracting lowest from highest.
Knowledge Check
Select the correct answer for each question. Click "Check Answer" to see if you are right.
Question 1
Which brackets are used to create a list in Python?
Question 2
Given fruits = ["apple", "banana", "cherry"], what does fruits[1] return?
Question 3
Which method adds an item to the end of a list?
Question 4
What does len([10, 20, 30, 40]) return?
Question 5
What does colours[-1] access in the list colours = ["red", "green", "blue"]?
Key Concepts Summary
- ●A list stores multiple items in a single variable using square brackets
[]. - ●Python uses zero-based indexing -- the first item is at index
0. - ●Use
.append()to add items to the end and.remove()to delete by value. - ●
len()returns the number of items;sum(),min(), andmax()work on numeric lists. - ●Negative indices count from the end:
-1is the last item.