Imagine you have a spaceport and need to store the names of all the planets for a flight. You could create a separate variable for each planet: planet1 = "Mars", planet2 = "Venus"… But that’s ridiculous! Lists were invented for exactly this kind of problem.
🎯 What Is a List?
A list is a box that can hold multiple values under a single name. Think of labeled slots in a cabinet!
# Instead of this:
planet1 = "Mars"
planet2 = "Venus"
planet3 = "Jupiter"
# Do this:
planets = ["Mars", "Venus", "Jupiter"]
One variable planets holds all three planets! 🚀
📝 How to Create a List
Lists are created with square brackets [ ]. Elements inside are separated by commas:
# A list of planets
planets = ["Mercury", "Venus", "Earth", "Mars"]
# A list of numbers (fuel levels at stations)
fuel_levels = [100, 75, 50, 25, 0]
# A list of emojis for the astronaut's mood
moods = ["😊", "😎", "🤔", "😴", "🚀"]
# An empty list (nothing added yet)
empty_cargo = []
Important: A single list can hold different data types, but that’s generally bad practice. It’s better to keep it homogeneous (all strings or all numbers).
🔢 Indexes: Element Addresses
Every element in a list has its own address number, called an index.
⚠️ The most important thing: In Python, numbering starts at 0, not 1!
crew = ["Yuri", "Neil", "Valentina", "Buzz"]
# 0 1 2 3
"Yuri"— index 0 (the first element!)"Neil"— index 1 (the second element)"Valentina"— index 2 (the third element)"Buzz"— index 3 (the fourth element)
🎯 Accessing an Element by Index
Use square brackets with the index to retrieve an element:
planets = ["Mercury", "Venus", "Earth", "Mars"]
print(planets[0]) # Mercury
print(planets[1]) # Venus
print(planets[2]) # Earth
print(planets[3]) # Mars
Practical example: picking a destination
destinations = ["Moon", "Mars", "Venus", "Titan"]
# Commander picks the first mission
first_mission = destinations[0]
print(f"First mission: fly to {first_mission}!") # fly to Moon!
# Second mission
second_mission = destinations[1]
print(f"Second mission: fly to {second_mission}!") # fly to Mars!
🔄 Negative Indexes
Python can count from the end! Use negative numbers:
crew = ["Alex", "Maria", "Ivan", "Olga"]
print(crew[-1]) # Last element → "Olga"
print(crew[-2]) # Second-to-last → "Ivan"
print(crew[-3]) # "Maria"
Diagram:
crew = ["Alex", "Maria", "Ivan", "Olga"]
0 1 2 3 ← regular indexes
-4 -3 -2 -1 ← negative indexes
📏 List Length: len()
The len() function tells you how many elements are in the list:
planets = ["Mercury", "Venus", "Earth", "Mars"]
print(len(planets)) # 4
supplies = ["Water", "Food", "Oxygen", "Fuel", "Tools"]
print(f"Cargo holds {len(supplies)} items") # Cargo holds 5 items
🎲 Lists + random.choice()
Remember random.choice()? It works with lists!
import random
destinations = ["Moon 🌙", "Mars 🔴", "Venus ☁️", "Jupiter 🌪️"]
# Pick a random planet to fly to
random_planet = random.choice(destinations)
print(f"Next mission: {random_planet}")
The program picks a random planet from the list every time it runs!
💡 Why Do We Need Lists?
1. Storing related data
# All planets in the solar system in one place
solar_system = [
"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"
]
2. Working with collections
# Targets for the end of the program
targets = [
"🌙 Moon",
"⭐ Star",
"🪐 Saturn",
"🌌 Galaxy"
]
# Show all options
print("Where are we flying?")
print(targets[0])
print(targets[1])
print(targets[2])
print(targets[3])
3. Random selection
import random
farewell_messages = [
"See you later, astronaut! 👋",
"Safe travels! 🚀",
"Come back in one piece! 🛸",
"See you in orbit! 🌍"
]
# A different farewell every time the program runs
print(random.choice(farewell_messages))
🎨 Interesting Examples
Mission status
mission_status = ["Preparing", "Launch", "In Flight", "Landing", "Complete"]
current_phase = 2 # We're currently in flight
print(f"Mission status: {mission_status[current_phase]}") # In Flight
Fuel levels
fuel_stations = [100, 85, 60, 40, 15]
# Check the first station
if fuel_stations[0] >= 50:
print("✅ First station is fueled!")
else:
print("❌ Refueling required!")
Ship crew
crew_members = ["Captain", "Pilot", "Engineer", "Scientist"]
print(f"Mission commander: {crew_members[0]}")
print(f"Co-pilot: {crew_members[1]}")
print(f"Total crew: {len(crew_members)} people")
⚠️ Common Mistakes
Mistake 1: IndexError
planets = ["Mars", "Venus", "Earth"]
print(planets[3]) # ❌ ERROR! Index 3 doesn't exist (only 0, 1, 2)
Remember: If a list has 3 elements, the valid indexes are 0, 1, 2. The last index is always len(list) - 1.
Mistake 2: Forgot the square brackets
# ❌ Wrong
planets = "Mars", "Venus", "Earth" # This is a tuple, not a list!
# ✅ Correct
planets = ["Mars", "Venus", "Earth"]
Mistake 3: Missing commas
# ❌ Wrong
crew = ["Alex" "Maria" "Ivan"] # SyntaxError!
# ✅ Correct
crew = ["Alex", "Maria", "Ivan"]
🎯 Summary
| What | How | Example |
|---|---|---|
| Create a list | [item1, item2] |
planets = ["Mars", "Moon"] |
| Get an element | list[index] |
planets[0] → "Mars" |
| Last element | list[-1] |
planets[-1] → "Moon" |
| List length | len(list) |
len(planets) → 2 |
| Random element | random.choice(list) |
random.choice(planets) |
Remember:
- ✅ Indexes start at 0
- ✅ Use square brackets [ ]
- ✅ Separate elements with commas
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!