0
0
Pythonprogramming~10 mins

Why sets are used in Python - See It in Action

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

The order of items in a set can vary, but all items will be unique.