Python, properly · Chapter 11 of 42
Sets
By the end of this chapter you can collect values with no repeats, ask "is this one of them?" in a single step, and compare two groups: what they share, what only one has, everything in either. 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
Farah asks three questions on Monday morning. Which areas do we deliver to? Which customers came back this week, which are new, and which did we lose? Can the Jain customers eat this dish? Each is about a group of distinct things: an area counts once however many orders it had, a customer is a regular or not.
With lists you would loop inside loops, compare item after item, and write your own code to skip repeats. A list allows repeats and keeps order, which is exactly what these questions do not want. Python has a type that is built for them, and it does each one in a single operator.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
areas.py: the areas we deliver to
The file starts with order_areas: the area of every order today, with repeats. Print how many different areas there are, then each area once, in alphabetical order, one per line. For the list given, exactly:
5 areas
Andheri
Borivali
Chembur
Goregaon
Powai
The check also runs your code with another list.
- Task 2
regulars.py: came back, new and lost
The file starts with two lists of customer ids, last_week and this_week (a customer can appear more than once). Print three lines: the customers in both weeks, the ones only in this week, and the ones only in last week, each sorted and separated by a comma and a space. For the lists given, exactly:
Came back: C003, C010
New: C044, C058
Lost: C017, C022, C031
The check also runs your code with other lists.
- Task 3
jain_safe.py: dishes a Jain customer can eat
The file starts with ingredients, a dictionary of dish name to the set of its ingredients, and avoid, the set of ingredients a Jain customer does not eat. Print the name of every dish with none of those ingredients, one per line, in the dictionary's order. For the data given, exactly:
Jain Dal
Jeera Rice
The check also runs your code with another avoid set.
- Task 4 · interview question
Interview: first_orders.py, repeats out, order kept
The file starts with orders: the customer id of each order today, in the order they came. Print each customer once, in the order of their first order, one per line. For the list given, exactly:
C031
C007
C052
C019
Use a set to remember the customers you have already printed (a set answers "seen it?" in one step). The check also runs your code with another list.
What you will learn
- Sets: a collection with no repeats and no order, and why.
- Membership: in on a set, one step however big the set.
- Set operators: union |, intersection &, difference - and symmetric difference ^.
- The traps: {} is not an empty set, a set has no order, and turning a list into a set loses its order.
More in Data and functions
Build "Sets" 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.