Lists
A list is an ordered, mutable collection of items. Lists are one of the most versatile data structures in Python — you'll use them constantly.
# Creating lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, True, 3.14]
empty = []
Accessing List Items
colors = ["red", "green", "blue", "yellow"]
print(colors[0]) # red (first item)
print(colors[-1]) # yellow (last item)
print(colors[1:3]) # ['green', 'blue'] (slicing)
Modifying Lists
fruits = ["apple", "banana", "cherry"]
# Add items
fruits.append("mango") # Add to end
fruits.insert(1, "grape") # Insert at index 1
# Remove items
fruits.remove("banana") # Remove by value
popped = fruits.pop() # Remove and return last item
del fruits[0] # Remove by index
# Change items
fruits[0] = "kiwi" # Replace item at index
Useful List Methods
numbers = [3, 1, 4, 1, 5, 9, 2]
numbers.sort() # [1, 1, 2, 3, 4, 5, 9]
numbers.reverse() # [9, 5, 4, 3, 2, 1, 1]
print(len(numbers)) # 7
print(numbers.count(1)) # 2 (how many 1s)
print(numbers.index(5)) # 1 (position of 5)
# Check if item exists
if 5 in numbers:
print("Found it!")
A compact way to create lists from existing ones:
squares = [x**2 for x in range(1, 6)]
# [1, 4, 9, 16, 25]
evens = [x for x in range(20) if x % 2 == 0]
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Tuples
Tuples are like lists but immutable — once created, they cannot be changed. Use tuples for data that shouldn't be modified.
# Creating tuples
coordinates = (10, 20)
rgb_color = (255, 128, 0)
single = (42,) # Note the trailing comma for single-item tuple
# Accessing items (same as lists)
print(coordinates[0]) # 10
print(rgb_color[-1]) # 0
# Unpacking tuples
x, y = coordinates
print(f"x={x}, y={y}") # x=10, y=20
coordinates[0] = 99 will raise a TypeError.
If you need to change values, use a list instead, or create a new tuple.
When to Use Tuples vs Lists
- Lists — for collections that change (shopping list, to-do items, user input)
- Tuples — for fixed data (coordinates, RGB colors, database rows, function return values)
Dictionaries
Dictionaries store data as key-value pairs. Instead of accessing items by index number, you access them by their key name.
# Creating a dictionary
user = {
"name": "Alice",
"age": 25,
"email": "alice@example.com",
"is_admin": False
}
# Accessing values
print(user["name"]) # Alice
print(user.get("age")) # 25
print(user.get("phone", "N/A")) # N/A (default if key doesn't exist)
Modifying Dictionaries
user = {"name": "Alice", "age": 25}
# Add or update
user["email"] = "alice@example.com" # Add new key
user["age"] = 26 # Update existing key
# Remove
del user["email"] # Remove by key
removed = user.pop("age") # Remove and return value
# Check if key exists
if "name" in user:
print("Name is set")
Iterating Over Dictionaries
scores = {"Alice": 95, "Bob": 87, "Charlie": 92}
# Loop through keys
for name in scores:
print(name)
# Loop through values
for score in scores.values():
print(score)
# Loop through both
for name, score in scores.items():
print(f"{name}: {score}")
# Output:
# Alice: 95
# Bob: 87
# Charlie: 92
Useful Dictionary Methods
config = {"host": "localhost", "port": 8080, "debug": True}
print(config.keys()) # dict_keys(['host', 'port', 'debug'])
print(config.values()) # dict_values(['localhost', 8080, True])
print(len(config)) # 3
# Merge two dictionaries
defaults = {"host": "localhost", "port": 80}
custom = {"port": 8080, "debug": True}
merged = {**defaults, **custom}
# {'host': 'localhost', 'port': 8080, 'debug': True}
Nested Data Structures
You can nest lists, tuples, and dictionaries inside each other:
# List of dictionaries (very common pattern)
users = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 28}
]
for user in users:
print(f"{user['name']} is {user['age']} years old")
# Dictionary with lists
student = {
"name": "Alice",
"grades": [95, 87, 92, 88],
"subjects": ["Math", "Science", "English"]
}
average = sum(student["grades"]) / len(student["grades"])
print(f"Average: {average}") # Average: 90.5
Practical Example: Contact Book
contacts = {}
def add_contact(name, phone):
contacts[name] = phone
print(f"Added {name}")
def find_contact(name):
if name in contacts:
print(f"{name}: {contacts[name]}")
else:
print(f"{name} not found")
def list_contacts():
if not contacts:
print("No contacts yet")
return
for name, phone in sorted(contacts.items()):
print(f" {name}: {phone}")
# Usage
add_contact("Alice", "555-0101")
add_contact("Bob", "555-0202")
find_contact("Alice") # Alice: 555-0101
list_contacts()
# Output:
# Alice: 555-0101
# Bob: 555-0202
Summary
- Lists
[]— Ordered, mutable collections. Use for data that changes. - Tuples
()— Ordered, immutable collections. Use for fixed data. - Dictionaries
{}— Key-value pairs. Use when you need to look up values by name. - List comprehensions provide a compact syntax for creating lists
- Nested structures let you model complex real-world data
inchecks membership in all three types
You now have a solid foundation in Python's core data structures. With variables, control flow, functions, and data structures under your belt, you're ready to build real Python programs. Keep practicing!