Module 6: Data Structures: Lists, Tuples, Sets, Dictionaries
Variables are great for storing single pieces of data. But what happens when you have a list of users, a collection of settings, or a set of unique tags? This module introduces data structures—the containers for your data.
1. Why We Need Data Structures
Imagine you're building a to-do list application. You could create a variable for each task:
This is clumsy. What if the user adds a fourth task? You can't just create `task4` in your code. You need a way to store a collection of items in a single variable. That's what **data structures** are for. They are specialized formats for organizing, processing, retrieving, and storing data.
🧠 Core Concept: The Filing Cabinet
Think of a data structure as a filing cabinet. Instead of leaving individual papers (data points) all over your desk, you organize them into folders, drawers, and labeled sections. Each type of data structure in Python is like a different kind of filing system, each with its own strengths for certain tasks.
2. Lists: The All-Purpose Workhorse
A **list** is an ordered, changeable collection of items. It's probably the most common data structure you'll use in Python. Lists are great when you need to store items in a specific sequence and expect to add, remove, or change items later.
- Ordered: Items have a defined position, which doesn't change.
- Mutable: You can change the list after it's created (add, remove, or modify items).
- Allows Duplicates: A list can contain multiple instances of the same item.
Creating and Accessing Lists
You create a list by placing items inside square brackets `[]`, separated by commas. To access an item, you use its **index**—its position in the list. Indexing in Python starts at **0**.
Modifying Lists
Because lists are mutable, you can easily change them.
3. Tuples: The Immutable Protectors
A **tuple** is an ordered, unchangeable collection of items. It's very similar to a list, but with one critical difference: once you create a tuple, you cannot change it. This property is called **immutability**.
- Ordered: Items have a defined position.
- Immutable: You CANNOT change the tuple after it's created.
- Allows Duplicates: A tuple can contain duplicates.
Creating and Using Tuples
You create a tuple using parentheses `()`. You access items just like with a list, but you can't assign a new value to an index.
Why use a tuple? Tuples are useful for data that should not be modified, like coordinates, configuration settings, or database records. They are also slightly more memory-efficient than lists.
4. Sets: The Unique and Unordered
A **set** is an unordered, mutable collection of **unique** items. Its primary characteristics are that it does not allow duplicate elements and it doesn't maintain any specific order.
- Unordered: Items have no defined position or index.
- Mutable: You can add or remove items from a set.
- No Duplicates: Every item in a set must be unique.
Creating and Using Sets
You create a set using curly braces `{}` or the `set()` function. Their main superpower is enforcing uniqueness.
Sets are excellent for membership testing (checking if an item is present) and for mathematical set operations like union and intersection.
5. Dictionaries: The Key-Value Store
A **dictionary** is an unordered (in Python versions before 3.7), mutable collection of **key-value pairs**. Instead of accessing items by a numerical index, you access them by a unique **key**. This is like a real-world dictionary where you look up a word (the key) to find its definition (the value).
- Key-Value Pairs: Each item consists of a key and an associated value.
- Mutable: You can add, remove, and change key-value pairs.
- Keys are Unique: Keys must be unique within a dictionary.
Creating and Using Dictionaries
You create a dictionary using curly braces `{}`, with each key-value pair written as `key: value`.
Dictionaries are incredibly useful for storing structured information where you need to look up values quickly without knowing their position.
6. Quick Summary Table
Characteristic | List | Tuple | Set | Dictionary |
---|---|---|---|---|
Syntax | `[1, 2, 3]` | `(1, 2, 3)` | `{1, 2, 3}` | `{'a': 1, 'b': 2}` |
Ordered | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes (since Py 3.7) |
Mutable | ✅ Yes | ❌ No | ✅ Yes | ✅ Yes |
Allows Duplicates | ✅ Yes | ✅ Yes | ❌ No | ✅ Yes (values), ❌ No (keys) |
You've Completed Module 6!
Amazing! You've just learned about the four fundamental data structures in Python. Understanding when to use a list, tuple, set, or dictionary is a key skill that separates beginner and intermediate programmers.
Now that you can store data in collections, our next module will focus on mastering a specific data type that's everywhere: strings. We'll also learn how to make our programs save and load data from files.
On to Module 7: Strings & File I/O →