Python for AILesson 3
Lesson 38 min
Lists & Dicts
Python's two workhorse data structures, plus comprehensions.
What you will learn
- ✓Lists vs dictionaries
- ✓List comprehensions
- ✓When to use each
Explanation
A list is an ordered collection: [82, 95, 71]. A dictionary maps keys to values: {'name': 'Mirza', 'score': 95} — perfect for labelled data.
List comprehensions transform or filter a list in one readable line, which you will see constantly in data code.
These two structures cover most everyday data handling before you reach NumPy and Pandas.
Code Example
python
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
Real-world use
Cleaning a dataset often starts as plain list/dict work before you load it into a DataFrame.
Common mistakes
- • Using a list when a dictionary (key lookup) would be far clearer and faster.
Practice
Given a list of prices, use a comprehension to keep only those under 50.
Knowledge check
0/2 answered1. Which structure is best for labelled key/value data?
2. What does [s for s in scores if s >= 80] do?
Answer all questions to check.