Python Creating Sets
Introduction
A set is one of Python’s built-in data structures used to store multiple unique values in a single variable. Sets are unordered, mutable, and do not allow duplicate elements.
Sets are commonly used when you need to store unique data and perform operations such as removing duplicates, membership testing, and mathematical set operations.
Sets are widely used in:
Data analysis
Automation testing
Database processing
Duplicate removal
User management systems
API response validation
Machine learning
In this tutorial, you will learn how to create sets in Python, different ways to create them, practical examples, real-world applications, common mistakes, and best practices.
What is a Set?
A set is an unordered collection of unique elements enclosed within curly braces {}.
Example
fruits = {"Apple", "Banana", "Mango"}
print(fruits)
Output
{'Apple', 'Banana', 'Mango'}
A set stores only unique values.
Creating a Simple Set
Syntax
set_name = {item1, item2, item3}
Example
colors = {"Red", "Green", "Blue"}
print(colors)
Output
{'Red', 'Green', 'Blue'}
Creating a Set of Numbers
Example
numbers = {10, 20, 30, 40, 50}
print(numbers)
Output
{10, 20, 30, 40, 50}
Creating a Set of Strings
Example
cities = {"Delhi", "Mumbai", "Chennai"}
print(cities)
Output
{'Delhi', 'Mumbai', 'Chennai'}
Creating a Mixed Data Type Set
A set can contain different data types.
Example
data = {"Python", 100, 99.5, True}
print(data)
Output
{True, 99.5, 100, 'Python'}
Creating an Empty Set
An empty set must be created using the set() function.
Correct
data = set()
print(data)
Output
set()
Why {} Does Not Create an Empty Set
Using {} creates a dictionary, not a set.
Example
data = {}
print(type(data))
Output
<class 'dict'>
Correct Way
data = set()
print(type(data))
Output
<class 'set'>
Creating a Set with Duplicate Values
Sets automatically remove duplicates.
Example
numbers = {10, 20, 30, 20, 10}
print(numbers)
Output
{10, 20, 30}
Only unique values are stored.
Creating a Set Using set()
The set() function converts other iterable objects into sets.
Example
numbers = set([1, 2, 3, 4])
print(numbers)
Output
{1, 2, 3, 4}
Creating a Set from a String
The set() function can create a set of unique characters.
Example
language = set("Python")
print(language)
Output
{'P', 'y', 't', 'h', 'o', 'n'}
Creating a Set from a Tuple
Example
numbers = set((10, 20, 30, 40))
print(numbers)
Output
{10, 20, 30, 40}
Creating a Set from a List with Duplicates
Example
numbers = [10, 20, 10, 30, 20]
unique_numbers = set(numbers)
print(unique_numbers)
Output
{10, 20, 30}
Creating a Frozen Set
A frozen set is an immutable version of a set.
Example
numbers = frozenset([10, 20, 30])
print(numbers)
Output
frozenset({10, 20, 30})
Creating Sets Using User Input
Example
name1 = input("Enter first name: ")
name2 = input("Enter second name: ")
names = {name1, name2}
print(names)
Sample Input
John
Alice
Output
{'John', 'Alice'}
Sets in Automation Testing
Sets are useful for storing unique test data.
Example: Unique Browser Names
browsers = {"Chrome", "Firefox", "Edge"}
print(browsers)
Output
{'Chrome', 'Firefox', 'Edge'}
Example: Unique Status Codes
status_codes = {200, 201, 400, 404, 500}
print(status_codes)
Output
{200, 201, 400, 404, 500}
Example: Remove Duplicate Test IDs
test_ids = {"TC001", "TC002", "TC001", "TC003"}
print(test_ids)
Output
{'TC001', 'TC002', 'TC003'}
Real-World Example: Remove Duplicate Email Addresses
emails = {
"user1@gmail.com",
"user2@gmail.com",
"user1@gmail.com"
}
print(emails)
Output
{'user1@gmail.com', 'user2@gmail.com'}
Real-World Example: Unique Student Names
students = {
"John",
"Alice",
"John",
"David"
}
print(students)
Output
{'John', 'Alice', 'David'}
Real-World Example: Unique Product Categories
categories = {
"Electronics",
"Furniture",
"Electronics"
}
print(categories)
Output
{'Electronics', 'Furniture'}
Real-World Example: Unique Cities
cities = {
"Delhi",
"Mumbai",
"Delhi",
"Chennai"
}
print(cities)
Output
{'Delhi', 'Mumbai', 'Chennai'}
Checking the Type of a Set
Example
data = {"Python", "Java"}
print(type(data))
Output
<class 'set'>
Finding the Length of a Set
The len() function returns the number of unique elements.
Example
fruits = {"Apple", "Banana", "Mango"}
print(len(fruits))
Output
3
Common Mistakes Beginners Make
Creating an Empty Set Using {}
Incorrect
data = {}
print(type(data))
Output
<class 'dict'>
Correct
data = set()
Expecting Duplicates to Be Stored
Example
numbers = {10, 10, 20, 20}
Output
{10, 20}
Sets automatically remove duplicates.
Assuming Sets Maintain Order
Incorrect Thinking
fruits = {"Apple", "Banana", "Mango"}
print(fruits)
The output order may vary.
Sets are unordered collections.
Best Practices
Use Sets for Unique Data
unique_users = {"John", "Alice", "David"}
Use set() for Duplicate Removal
numbers = [1, 2, 2, 3]
unique_numbers = set(numbers)
Use Meaningful Variable Names
employee_ids = {101, 102, 103}
Use Frozen Sets for Immutable Data
config = frozenset(["QA", "UAT"])
Avoid Relying on Element Order
Sets do not guarantee insertion order.
Advantages of Sets
Automatically remove duplicates
Fast membership testing
Efficient data processing
Support mathematical set operations
Useful for unique data storage
Improve performance for searches
Limitations of Sets
Unordered collection
Cannot access elements using indexes
Mutable (except frozenset)
Only hashable elements can be stored
Conclusion
Sets are a powerful Python data structure used for storing unique values efficiently. They automatically eliminate duplicates and provide fast lookup operations, making them ideal for data cleaning, validation, and membership testing.
Whether you are removing duplicate records, validating API responses, managing users, or processing datasets, sets offer a simple and efficient solution.
Understanding set creation is essential before learning set methods, set operations, and advanced data manipulation techniques.
Frequently Asked Questions (FAQs)
What is a set in Python?
A set is an unordered collection of unique elements.
Example:
fruits = {"Apple", "Banana", "Mango"}
How do I create an empty set?
Use the set() function.
data = set()
Output:
set()
Can a set contain duplicate values?
No.
Sets automatically remove duplicate values.
numbers = {1, 1, 2, 2, 3}
Output:
{1, 2, 3}
Can a set store different data types?
Yes.
data = {"Python", 100, True, 99.5}
What is a frozenset?
A frozenset is an immutable version of a set.
numbers = frozenset([1, 2, 3])
Key Takeaways
Sets store unique values only.
Sets are created using curly braces
{}or theset()function.Empty sets must be created using
set().Duplicate values are automatically removed.
Sets are unordered collections.
The
set()function converts other iterables into sets.frozensetcreates an immutable set.Sets are widely used in automation testing and real-world applications.
Sets are ideal for duplicate removal and membership testing.
Understanding set creation is essential before learning set methods and set operations.
