Skip to main content

Introduction to Collections — Overview Only

What Are Collections and Why Do They Matter?

In the previous lessons of this chapter, you learned about single data types: int (a number), str (text), bool (true/false), and None (nothing). These work great for storing ONE piece of data at a time.

But what if you need to store multiple related pieces of data together? For example:

  • A list of shopping items: milk, eggs, bread, cheese
  • A student's test scores: 95, 87, 92, 88
  • A person's contact info: name, email, phone number

Collections are Python's way of grouping related data together in one variable. Think of them as containers—each one organized slightly differently depending on what you're storing.

In this lesson, you'll meet all four main collection types. We'll see what each one looks like and when you'd use it. We won't dive deep into each one yet—that happens in Chapters 18-19. This lesson is about awareness: knowing they exist and understanding their purpose.

The Four Core Collection Types

1. list — Ordered, Changeable, Multiple Items

A list stores multiple items in a specific order. You can change, add, or remove items after creating it.

When to use: When you have a sequence of similar items where order matters, and you might change what's in the list.

What it looks like:

shopping_list: list[str] = ["milk", "eggs", "bread"]
scores: list[int] = [95, 87, 92, 88]
mixed: list = [1, "hello", 3.14] # Can mix types, but not recommended

Pronunciation: "List of strings", "List of integers"

Simple example:

# Create a list of student names
students: list[str] = ["Alex", "Bob", "Charlie"]

# Print the list
print(students) # Output: ['Alex', 'Bob', 'Charlie']

Real-world analogy: A list is like a shopping list. Items are in order. You can cross one off (remove), add a new item, or change one. Each spot in the list holds one item.

2. tuple — Ordered, Unchangeable, Multiple Items

A tuple stores multiple items in a specific order, like a list. But once you create it, you CANNOT change it. It's fixed.

When to use: When you have a fixed set of related items that shouldn't change. Tuples are often used to return multiple values from functions (you'll learn about this in Chapter 20).

What it looks like:

rgb_color: tuple[int, int, int] = (255, 0, 128)
coordinates: tuple[float, float] = (3.5, 7.2)
person_info: tuple[str, int] = ("Alex", 25)

Pronunciation: "Tuple of integers (three of them)", "Tuple of floats (two of them)"

Simple example:

# GPS coordinates don't change after you record them
location: tuple[float, float] = (40.7128, -74.0060) # NYC

print(location) # Output: (40.7128, -74.006)

Real-world analogy: A tuple is like a fixed photograph. Once you take it, you can't change what's in it. The order of items is set. You can look at it, but you can't modify it.

3. dict — Key-Value Pairs, Changeable

A dict (dictionary) stores data as pairs: a key and its value. Instead of accessing items by position like a list, you look them up by name (key).

When to use: When you need to store related information that you'll look up by name. Like a real dictionary where you look up a word (key) to find its definition (value).

What it looks like:

student: dict[str, str] = {"name": "Alex", "grade": "A"}
prices: dict[str, float] = {"apple": 1.99, "banana": 0.59, "orange": 2.49}
ages: dict[str, int] = {"Alice": 30, "Bob": 25, "Charlie": 28}

Pronunciation: "Dictionary with string keys and string values", "Dictionary with string keys and float values"

Simple example:

# Store information about a person
person: dict[str, str] = {
"name": "Alex",
"email": "[email protected]",
"city": "New York"
}

print(person) # Output: {'name': 'Alex', 'email': '[email protected]', 'city': 'New York'}

Real-world analogy: A dict is like a phone book. You don't remember that your friend's number is in position 47. You remember their name is "Alice". You look up "Alice" (the key) and find her number (the value).

4. set — Unique Values Only, Unordered

A set stores unique values with no duplicates. Order doesn't matter, and you can't access items by position.

When to use: When you need to ensure no duplicates exist, or when you need to check "is this item in my collection?" quickly. Duplicates are automatically removed.

What it looks like:

unique_ids: set[int] = {101, 102, 103}
tags: set[str] = {"python", "coding", "ai"}
numbers: set[int] = {1, 2, 3, 1, 2} # Duplicates automatically removed

Pronunciation: "Set of integers", "Set of strings"

Simple example:

# List of product IDs (some repeated)
product_ids: set[int] = {1, 2, 3, 2, 1, 4}

print(product_ids) # Output: {1, 2, 3, 4}
# Notice: only unique values remain, duplicates are gone

Real-world analogy: A set is like a container of unique badges. If you already have the Python badge, adding another Python badge has no effect—you still have just one. There are no duplicates.

Understanding Type Hints for Collections

Remember from Chapter 13? Type hints help us write clear code. They're especially important for collections because they tell readers (and AI partners) what kind of data is inside.

How Type Hints Work for Collections

# This reads as: "scores is a list of integers"
scores: list[int] = [95, 87, 92]

# This reads as: "prices is a dictionary with string keys and float values"
prices: dict[str, float] = {"apple": 1.99, "banana": 0.59}

# This reads as: "location is a tuple of two floats"
location: tuple[float, float] = (40.7128, -74.0060)

# This reads as: "tags is a set of strings"
tags: set[str] = {"python", "coding", "ai"}

Why type hints matter for collections:

  1. Clarity: Anyone reading your code (including future you) instantly knows what's inside
  2. AI Partnership: When you ask Claude Code or Gemini for help, the type hints make your intent crystal clear
  3. Error Prevention: Type hints help catch mistakes early

Quick Reference: Which Collection Should You Use?

CollectionOrdered?Changeable?Best ForExample
listYesYesSequences that might changeshopping_items: list[str]
tupleYesNoFixed sets of related valuesrgb: tuple[int, int, int]
dictNoYesName-based lookupsphone_book: dict[str, str]
setNoYesUnique values, no duplicatesuser_ids: set[int]

Three Code Examples

Example 1: Basic Collections with Type Hints

# A list of integers (in order, can change)
numbers: list[int] = [1, 2, 3, 4, 5]

# A tuple of floats (in order, fixed)
coordinates: tuple[float, float] = (3.5, 7.2)

# A dictionary with string keys and string values (lookup by name)
student: dict[str, str] = {"name": "Alex", "grade": "A"}

# A set of unique integers (no duplicates, unordered)
unique_ids: set[int] = {101, 102, 103}

print(numbers) # [1, 2, 3, 4, 5]
print(coordinates) # (3.5, 7.2)
print(student) # {'name': 'Alex', 'grade': 'A'}
print(unique_ids) # {101, 102, 103}

Example 2: Type Hints Describe Collections

# Each type hint tells us what's inside the collection

# List of strings - storage for multiple text items
names: list[str] = ["Alice", "Bob", "Charlie"]

# Dictionary mapping strings to floats - name-based price lookup
prices: dict[str, float] = {"apple": 1.99, "banana": 0.59}

# Tuple of integers - fixed coordinates or RGB color
color_rgb: tuple[int, int, int] = (255, 0, 128)

# Set of unique strings - tags with no duplicates
skills: set[str] = {"python", "coding", "testing"}

Example 3: When to Use Each Collection

# LIST: Order matters, items might change
shopping_list: list[str] = ["milk", "eggs", "bread"]
# You might cross items off or add more

# TUPLE: Order matters, items are fixed
rgb_color: tuple[int, int, int] = (255, 0, 128)
# RGB values for a specific color don't change

# DICT: Look up by name (key)
phone_book: dict[str, str] = {
"Alice": "555-1234",
"Bob": "555-5678"
}
# You find someone's number by their name, not by position

# SET: Only unique values, no duplicates
attended_conferences: set[str] = {"PyConf", "AI Summit"}
# Removes duplicates automatically if you add "PyConf" twice

Important Notice: Deep Dive Coverage Coming

This lesson is awareness only. You now know:

  • The four main collection types exist
  • Their basic purpose and when to use each
  • How to read and write type hints for collections
  • What simple examples look like

You do NOT need to know:

  • How to add or remove items from collections (methods)
  • How to loop through collections
  • Advanced operations on each type

These skills come next in:

  • Chapter 18: Lists, Tuples, and Dictionary — Deep dive into list and dict with all their methods and operations
  • Chapter 19: Set, Frozen Set, and GC — Deep dive into set with operations and performance

When you reach those chapters, you'll return to collections and learn everything you can DO with them.

For now, your job is just to recognize them and understand their purpose.

Try With AI

Use your AI companion (Claude Code or Gemini CLI). These questions help you deepen your understanding of collections before moving forward.

Prompt 1: Understanding Collection Types

I see four collection types in Python: list, tuple, dict, and set.
What is the main difference between each one? When would I use list
instead of tuple? When would I use dict instead of list?

Expected outcome: AI explains the four types with clear distinctions (ordered vs unordered, changeable vs unchangeable, indexed vs key-based, duplicates vs unique).

Prompt 2: Reading Type Hints for Collections

I see type hints like list[int], dict[str, float], and tuple[float, float].
What do these mean? What is the part inside the square brackets?
How does type[int] differ from type[str]?

Expected outcome: AI explains that the part in square brackets describes what's inside the collection (the element type), with clear examples showing what each means.

Prompt 3: Choosing the Right Collection

I need to store:
1. A list of student names (might add or remove students)
2. Latitude and longitude coordinates (never change)
3. A phone book where I look up names to find numbers
4. A collection of unique product IDs with no duplicates

Which collection type should I use for each? Why?

Expected outcome: AI selects list, tuple, dict, and set respectively, explaining the reasoning for each choice.

Prompt 4: Anticipating Chapter 18-19

I know collections exist and why they matter. What questions should I be
asking about these collection types that I'll learn the answers to in
Chapters 18 and 19? What can I DO with these collections?

Expected outcome: AI lists operations like adding items to lists, removing items, looping through collections, checking membership, etc.—preview of what you'll learn soon.

Validation Checkpoint:

  • Can you identify the four collection types by sight?
  • Can you read and interpret type hints for collections?
  • Can you match a collection type to a real-world scenario?
  • Can you explain why tuple is immutable and when that matters?

If you answered yes to all four, you've mastered Lesson 4 (awareness level).

Safety & Ethics Note: In this lesson, we're learning to organize and store data safely with clear types. As you progress to Chapters 18-19 and beyond, you'll learn how collections are accessed and modified. Always remember: more powerful operations need more careful thinking. When you work with collections containing user data or sensitive information, you'll need to validate and protect that data. We'll address this in later chapters.

Next: You've now completed awareness of all core Python data types. Lesson 5 (the capstone for Chapter 14) will bring everything together in a hands-on project where you use variables, type hints, core types, and collections to build an interactive type explorer.