Sets
You’ve made it to the final major data structure in our "Big Four" toolkit! We’ve handled Lists, Dictionaries, and Tuples. Now, we have Sets.
I know, I know—diving into another data type can feel a bit dry. But as we’ve discussed, you don't need to be a walking encyclopedia of syntax. The goal isn't to memorize every method; it’s to know that a process exists so that when you face a specific problem, you know exactly which tool to grab from your belt. Real programming is about research and mindset, not just rote memory.
What is a set?
A Set is a collection that is unordered, unindexed, and—most importantly contains no duplicate members.
Think of a set like a physical bag of labeled marbles. You can reach in and grab one, but they aren't sitting in a neat line (unordered), and you can’t have two marbles that are exactly the same.
Set Syntax
Sets are written with curly braces {}, just like dictionaries, but they don't have "keys" or "colon pairs." They just have items.
- Print my_set.
- Print numbers.
Basic Set Methods
Since you can't use an index to change an item, you use these methods to manage the items in your set:
- .add(): Adds an item to the set.
- .remove(): Removes a specific item (raises an error if the item is not found).
- .discard(): Removes an item (does not raise an error if the item is missing).
- .clear(): Empties the entire set.
- Print skills.
- Remove HTML from skills and print again.
- Discard C++ from skills and print again.
- Clear skills and print.
Because sets are unordered, we cannot find data using slicing. However, we can use the in keyword to see if a specific value is in the set.
- Print C++ in skills.
One last caveat: because sets only store unique values, if we have a set nums = {2, 3, 3, 4, 4, 4, 6, 6, 6, 6, 6, 6} and we check the length using len(nums), we get 4, not 12. This is because Python ignores all those duplicate entries.
Scenario: Removing Duplicates
We can use the set() function to return a set with no duplicates from a list that has repeated items:
- print unique_items.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named sets.py in your part34 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Set Creation
-
Task: Create a set named
coding_languagescontaining "Python", "JavaScript", and "C++". - Goal: Print the set and notice that the order of items is not the same as how you typed them.
Exercise 2: Adding and Removing
-
Task: Create a set
fruits = {"apple", "banana"}. -
Goal: Add "orange" using
.add()and remove "apple" using.remove(). Print the final set.
Exercise 3: Membership Testing
-
Task: Create a set
prime_numbers = {2, 3, 5, 7, 11}. -
Goal: Use the
inkeyword to check if5is in the set and print the result (True/False).
Exercise 4: The Length Caveat
-
Task: Create a set
test_scores = {90, 85, 90, 70, 85, 100}. -
Goal: Use
len()to find the length of the set and print it. Observe why the number is smaller than the items typed.
Exercise 5: Instant Deduplication
-
Task: Create a list
names = ["Sarah", "John", "Sarah", "Pete", "John"]. - Goal: Convert this list into a set to remove duplicates, then convert it back into a list. Print the final list.
Don't Forget to commit and Push!