A set is a collection with two rules: no duplicates, and no guaranteed order. Use one when you care about what's in the group, not how many times or in what sequence.
The classic use: strip duplicates out of a list instantly.
Sets
PYTHON
languages = {"Twi", "Yoruba", "Twi"}
print(languages) # duplicate collapses
print("Twi" in languages) # membership test
≈
JAVASCRIPT
const languages = new Set(["Twi", "Yoruba", "Twi"]);
console.log(languages); // duplicate collapses
console.log(languages.has("Twi")); // membership test
Compare → Python uses curly braces (like dictionaries, but with no colons) or the set() function. JavaScript needs new Set(...). Membership uses the `in` keyword in Python and.has() in JavaScript. Same concept, different construction.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Make your own list of names with some repeats, then print how many signups there were and how many different people signed up.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
◈ Graded Challenge
Create a list named businesses containing three records, each with a name and a city. Then create count holding how many there are, and second_name holding the name of the second business.
Name it exactly: businesses, count, second_name
3 visible + 2 hidden
loading editor…
Mark this lesson complete
Signed in, your progress follows you to every device.