Data Structures - Dictionaries and Sets
Learn how to store key-value pairs and unique collections of data
Learning Objectives
- Create and use Python dictionaries
- Access, add, and remove dictionary items
- Understand dictionary keys and values
- Learn how to use Python sets for unique items
- Perform basic set operations (union, intersection)
Key-Value Storage
Sometimes, you don’t want to store items in a numbered list. Instead, you want to label them. For example, a “user” has a “name”, an “email”, and an “age”. In Python, we use Dictionaries for this.
Python Dictionaries
A Dictionary is a collection which is ordered (as of Python 3.7), changeable, and does not allow duplicates. Dictionaries are written with curly braces {} and have keys and values.
|
|
Modifying Dictionaries
|
|
Python Sets
A Set is an unordered collection with no duplicate elements. Sets are useful for removing duplicates from other collections and performing mathematical operations.
|
|
Interactive Practice
Try creating a dictionary representing your favorite car (brand, model, year).
| Feature | Dictionary {k:v} |
Set {} |
|---|---|---|
| Access | via Key | No index/key |
| Mutable | Yes | Yes |
| Duplicates | Keys: No, Values: Yes | No |
| Order | Ordered (3.7+) | Unordered |
Quiz
Complete this quiz with a minimum score of 80% to mark Day 7 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!