Data Structures

Chapter 4: Data Structures

Data structures organize and store collections of values efficiently. Instead of managing individual variables, you group related data together in containers designed for specific use cases. Python provides four fundamental data structures: lists for ordered mutable collections, tuples for ordered immutable sequences, dictionaries for key-value mappings, and sets for unique unordered elements.

Choosing the right data structure makes programs faster, clearer, and easier to maintain. For example, lists work perfectly for shopping carts where items can change, while dictionaries excel at storing user profiles with name-value pairs. Understanding these structures is essential—they appear in virtually every Python program you'll write.

Lists: Ordered Mutable Collections

Lists store ordered sequences of items that you can modify. Create lists with square brackets, separating items with commas:

Access items by index (position), starting from 0:

Lists are mutable—you can change, add, or remove items after creation. This flexibility makes lists the most frequently used data structure in Python:

Essential List Methods

The append() method adds items to the end:

fruits = ["apple"]
fruits.append("banana")
fruits.append("cherry")
print(fruits)  # Output: ['apple', 'banana', 'cherry']

The insert() method adds items at specific positions:

fruits = ["apple", "cherry"]
fruits.insert(1, "banana")  # Insert at index 1
print(fruits)  # Output: ['apple', 'banana', 'cherry']

Remove items with remove() (by value) or pop() (by index):

fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")  # Removes first occurrence
print(fruits)  # Output: ['apple', 'cherry']

last = fruits.pop()  # Removes and returns last item
print(last)    # Output: cherry
print(fruits)  # Output: ['apple']

Check if items exist with the in operator, and get list length with len():

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)  # Output: True
print(len(fruits))          # Output: 3

Tuples: Immutable Sequences

Tuples are like lists but immutable—once created, you can't change them. Create tuples with parentheses:

coordinates = (10, 20)
rgb_color = (255, 128, 0)
person = ("Alice", 30, "Engineer")

Access tuple items by index just like lists:

coordinates = (10, 20)
x = coordinates[0]  # 10
y = coordinates[1]  # 20

The key difference: tuples can't be modified. Attempting to change items raises an error:

coordinates = (10, 20)
# coordinates[0] = 15  # Error: tuples don't support item assignment

Use tuples for data that shouldn't change: geographic coordinates, RGB colors, database records. The immutability provides safety—you know the values won't accidentally change. Tuples are also slightly faster than lists and can be used as dictionary keys (lists cannot).

Tuple unpacking lets you assign multiple variables at once:

coordinates = (10, 20)
x, y = coordinates  # Unpacks into separate variables
print(x)  # Output: 10
print(y)  # Output: 20

Dictionaries: Key-Value Mappings

Dictionaries store data as key-value pairs, letting you look up values by meaningful keys instead of numeric indices. Create dictionaries with curly braces:

user = {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
}

prices = {
    "apple": 0.50,
    "banana": 0.30,
    "cherry": 0.75
}

Access values using keys in square brackets:

user = {"name": "Alice", "age": 30}
print(user["name"])  # Output: Alice
print(user["age"])   # Output: 30

Add or modify entries by assigning to keys:

user = {"name": "Alice"}
user["age"] = 30        # Add new key-value pair
user["name"] = "Bob"    # Modify existing value
print(user)  # Output: {'name': 'Bob', 'age': 30}

Dictionary Methods and Safety

The get() method safely retrieves values, returning None (or a default) if the key doesn't exist:

user = {"name": "Alice"}
age = user.get("age")      # Returns None (key doesn't exist)
age = user.get("age", 25)  # Returns 25 (custom default)

Iterate over dictionaries with keys(), values(), or items():

prices = {"apple": 0.50, "banana": 0.30}

for fruit in prices.keys():
    print(fruit)  # Prints: apple, banana

for price in prices.values():
    print(price)  # Prints: 0.50, 0.30

for fruit, price in prices.items():
    print(f"{fruit}: ${price}")  # Prints: apple: $0.50, banana: $0.30

Dictionaries excel when you need to associate related data or perform fast lookups by meaningful identifiers. They're perfect for configuration settings, caching, counting occurrences, and representing structured data.

Sets: Unique Unordered Collections

Sets store unique elements with no duplicates. Create sets with curly braces (like dictionaries, but without key-value pairs):

unique_numbers = {1, 2, 3, 4, 5}
fruits = {"apple", "banana", "cherry"}

Sets automatically remove duplicates:

numbers = {1, 2, 2, 3, 3, 3}
print(numbers)  # Output: {1, 2, 3}

Sets are unordered—items don't have positions or indices. You can't access set items by index, but you can check membership efficiently:

fruits = {"apple", "banana", "cherry"}
print("apple" in fruits)   # Output: True
print("grape" in fruits)   # Output: False

Add items with add(), remove with remove() or discard():

fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits)  # Output: {'apple', 'banana', 'cherry'}

fruits.remove("banana")  # Raises error if item doesn't exist
fruits.discard("grape")  # No error if item doesn't exist

Sets support mathematical operations like union, intersection, and difference:

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)  # Union: {1, 2, 3, 4, 5}
print(a & b)  # Intersection: {3}
print(a - b)  # Difference: {1, 2}

Use sets to remove duplicates from lists, test membership efficiently, or perform set operations on collections.

Choosing the Right Data Structure

  • Lists: Use for ordered collections that change (shopping carts, task lists, search results)
  • Tuples: Use for fixed data that shouldn't change (coordinates, RGB values, database rows)
  • Dictionaries: Use for key-value associations (user profiles, configuration, caching)
  • Sets: Use for unique elements or membership testing (tags, unique visitors, deduplication)

Try It Yourself: Practice Exercises

Exercise 1: Shopping Cart
Create a list of items in a shopping cart. Add three items, remove one, and print the final list and its length.

Exercise 2: Student Records
Create a dictionary representing a student with keys: "name", "age", "grade", and "courses" (a list). Access and print each value.

Exercise 3: Remove Duplicates
Given the list [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], convert it to a set to remove duplicates, then back to a sorted list.

Exercise 4: Word Counter
Create a dictionary that counts how many times each word appears in: ["apple", "banana", "apple", "cherry", "banana", "apple"]. Use a loop and dictionary access.

Exercise 5: Tuple Coordinates
Create a tuple representing a point (x, y, z) in 3D space. Use tuple unpacking to assign x, y, and z to separate variables and print them.

Exercise 6: Set Operations
Create two sets of numbers. Find their union, intersection, and elements unique to the first set.

What's Next?

You've mastered Python's fundamental data structures: lists for mutable sequences, tuples for immutable ones, dictionaries for key-value pairs, and sets for unique elements. These structures form the foundation of data organization in Python.

In the next chapter, we'll explore modules and packages—how to organize code into reusable components and leverage Python's vast ecosystem of libraries. You'll learn to import functionality, structure larger projects, and use third-party packages effectively.

Continue to Chapter 5: Modules and Packages

📝 Test Your Knowledge: Data Structures

Take this quiz to reinforce what you've learned in this chapter.