🐍beginner
Python Lists — Collections of Data
Learn how to store, access, and manipulate groups of data with Python lists — one of the most important tools in programming.
What is a List?
A variable can only hold one thing — one name, one number. But what if you have 20 students in a class? You can't make 20 separate variables! That's where lists come in.
A list is like a numbered shelf. Each slot holds one item, and you can add things, remove things, rearrange things, or look at any slot by its number. Lists are one of the most-used features in Python — you'll use them in almost every program you write.
Creating and Accessing Lists
python
# Create a list with square brackets []
colors = ["red", "blue", "green", "yellow"]
scores = [95, 87, 92, 78, 100]
mixed = ["hello", 42, True, 3.14] # can mix types!
# Access items by index (position number)
# IMPORTANT: counting starts at 0, not 1!
print(colors[0]) # red (first item)
print(colors[1]) # blue (second item)
print(colors[-1]) # yellow (last item!)
# How many items?
print(len(colors)) # 4
# Is something in the list?
print("blue" in colors) # True
print("purple" in colors) # FalseWhy Does Counting Start at 0?
This confuses everyone at first! In Python (and most programming languages), the first item is at position 0, not position 1.
Think of it like measuring distance: the first item is at the START (0 steps from the beginning), the second item is 1 step away, and so on.
So for colors = ['red', 'blue', 'green']:
• colors[0] = 'red' (1st item)
• colors[1] = 'blue' (2nd item)
• colors[2] = 'green' (3rd item)
• colors[3] = ERROR! (there's no 4th item)
Adding, Removing, and Changing Items
python
fruits = ["apple", "banana"]
# Add to the end
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'mango']
# Insert at a specific position
fruits.insert(1, "grape") # put grape at index 1
print(fruits) # ['apple', 'grape', 'banana', 'mango']
# Remove by value
fruits.remove("banana")
print(fruits) # ['apple', 'grape', 'mango']
# Remove by index
removed = fruits.pop(0) # removes and returns first item
print(removed) # apple
print(fruits) # ['grape', 'mango']
# Change an item
fruits[0] = "watermelon"
print(fruits) # ['watermelon', 'mango']Looping Through Lists
python
students = ["Aarav", "Priya", "Zain", "Meera"]
# Simple loop
for student in students:
print(f"Hello, {student}!")
# Loop with index using enumerate()
for i, student in enumerate(students):
print(f"{i + 1}. {student}")
# Output:
# 1. Aarav
# 2. Priya
# 3. Zain
# 4. Meera
# Useful list tricks
numbers = [3, 1, 4, 1, 5, 9]
print(sorted(numbers)) # [1, 1, 3, 4, 5, 9]
print(sum(numbers)) # 23
print(max(numbers)) # 9
print(min(numbers)) # 1Pro Tip
Use descriptive plural names for lists: 'students' (not 'student'), 'scores' (not 'score'). This makes your code read naturally: 'for student in students' sounds like English!
Try It Yourself
Create a list of 5 of your favorite movies. Then: (1) print how many movies are in the list, (2) add a new movie to the end, (3) remove the second movie, (4) use a for loop to print each movie numbered like '1. Movie Name', and (5) sort the list alphabetically and print it.
Ready to build?
Put what you learned into practice — pick a project and start coding.
Start Building Free