Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why sets are used
๐ Scenario: Imagine you have a list of fruits you bought from the market, but some fruits are repeated. You want to find out which unique fruits you have without any duplicates.
๐ฏ Goal: Learn how to use a set in Python to find unique items from a list and understand why sets are useful.
๐ What You'll Learn
Create a list called fruits with some repeated fruit names
Create a set called unique_fruits from the fruits list
Print the unique_fruits set to see only unique fruit names
๐ก Why This Matters
๐ Real World
Sets help when you want to find unique things like unique customers, unique products, or unique words in a text.
๐ผ Career
Understanding sets is important for data cleaning, removing duplicates, and improving program efficiency in many programming jobs.
Progress0 / 4 steps
1
Create a list with repeated fruits
Create a list called fruits with these exact values: 'apple', 'banana', 'apple', 'orange', 'banana', 'grape'.
Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Create a set from the list
Create a set called unique_fruits by converting the fruits list into a set using the set() function.
Python
Hint
Use the set() function to remove duplicates from the list.
3
Print the unique fruits
Use print() to display the unique_fruits set so you can see the unique fruit names without duplicates.
Python
Hint
Use print() to show the contents of the set.
4
Understand the output
Run the program and observe the output. Notice that the printed set shows only unique fruit names without any repeats.
Python
Hint
The order of items in a set can vary, but all items will be unique.
Practice
(1/5)
1. Which of the following is the main reason to use a set in Python?
easy
A. To store unique items without duplicates
B. To keep items in a specific order
C. To allow duplicate values
D. To store key-value pairs
Solution
Step 1: Understand the purpose of sets
Sets automatically remove duplicate items, so they only keep unique elements.
Step 2: Compare with other data types
Lists allow duplicates and keep order, dictionaries store key-value pairs, so they don't fit the main use of sets.
Final Answer:
To store unique items without duplicates -> Option A
Quick Check:
Sets = Unique items [OK]
Hint: Sets always keep unique items, no duplicates allowed [OK]
Common Mistakes:
Thinking sets keep order
Confusing sets with lists or dictionaries
Assuming sets allow duplicates
2. Which of the following is the correct way to create a set in Python?
easy
A. my_set = {1, 2, 3}
B. my_set = [1, 2, 3]
C. my_set = (1, 2, 3)
D. my_set = {'a': 1, 'b': 2}
Solution
Step 1: Recall set syntax
Sets are created using curly braces with comma-separated values, like {1, 2, 3}.
Step 2: Identify other data types
Square brackets create lists, parentheses create tuples, and curly braces with key-value pairs create dictionaries.
Final Answer:
my_set = {1, 2, 3} -> Option A
Quick Check:
Curly braces with values = set [OK]
Hint: Use curly braces with values to create sets [OK]