Python, properly · Chapter 12 of 42
Comprehensions
By the end of this chapter you can build a new list, dictionary or set from an old one in a single readable line, filter as you go, and tell when a plain loop is the better choice. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data and functions
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Half of the scripts you have written so far have the same four lines: make an empty list, loop, maybe test something, append. Bhavna wants the spicy dishes for a warning sticker, Rafiq wants a price lookup with only what is on sale today, and the customer emails need tidying. Each is "take every item, maybe keep it, maybe change it, collect the results".
The four-line loop works, but the point of the code, what you collect, is buried in the bookkeeping: the empty list, the append. Python has a shorter form that says the point in one line, and every Python codebase you will read uses it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
spicy.py: the spicy dishes
The file starts with dishes: a list of (name, spice) pairs, spice from 0 to 3. With a list comprehension, build a list called spicy with the names of the dishes whose spice is 2 or more, in the same order, and print it. For the data given, exactly:
['Dal Tadka', 'Chicken Curry', 'Misal Pav']
The check reads your spicy list and runs your code with other dishes.
- Task 2
price_index.py: a lookup of what is on today
The file starts with menu: a list of dish records, each a dictionary with id, price and available. With a dictionary comprehension, build price_of, mapping the id of every available dish to its price, in menu order, and print it. For the data given, exactly:
{'chole': 150, 'butter-naan': 40, 'egg-curry': 180}
The check reads your price_of and runs your code with a changed menu.
- Task 3
clean_emails.py: one email per customer
The file starts with emails, typed in by hand: some have capitals, some have spaces around them, and some customers appear twice. With a set comprehension, build unique_emails, the emails with no spaces around them and all lowercase. Print how many there are, then each one in alphabetical order, one per line. For the data given, exactly:
4 customers
dhruv.shah@example.com
kavya.pawar@example.com
neel.naik@example.com
zara.gupta@example.com
The check reads your unique_emails and runs your code with other emails.
- Task 4 · interview question
Interview: flatten.py, one list from many
The file starts with orders: a list of orders, each a list of dish ids. With one list comprehension that has two for parts, build all_items, every dish of every order in order, then print it and the number of items. For the data given, exactly:
['roti', 'dal-tadka', 'roti', 'veg-thali', 'jeera-rice', 'roti']
6 items
The check reads your all_items and runs your code with other orders.
What you will learn
- List comprehensions: [expression for item in items], and the loop it replaces.
- Filtering with if, and choosing a value with x if test else y.
- Dictionary and set comprehensions, and a comprehension with two fors.
- When not to use one, and what happens inside.
More in Data and functions
Build "Comprehensions" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.