Course Content
Python Data Structures
Python Data Structures
Creating a Set
A set in Python is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate elements, making them ideal for operations like removing duplicates or performing mathematical set operations. Sets are mutable, meaning their elements can be added or removed, but the data inside the set must be immutable (e.g., numbers, strings, or tuples).
Note
A set cannot contain dictionaries or lists because both are mutable data types.
There are two main ways to create a set in Python:
- Using curly braces
{}
with elements separated by commas; - Using the
set()
function.
Here are some key points about sets:
- They are mutable: you can add or remove elements from a set, but the set itself must contain immutable elements;
- Duplicate Elements: if duplicate elements are added to a set, they are automatically removed;
- Unordered: the order of elements in a set is not guaranteed and may vary;
- Diverse Data Types: sets can contain elements of different data types, such as strings, integers, or tuples.
Let's define a set using the set()
function:
# Creating a set which contains strings movie_set = set("Interstellar") print(movie_set)
Next, let's define a set of popular movies using curly braces:
# Creating a set of movies using curly braces movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Tenet"} print(movies)
Duplicate entries like "Tenet"
are removed automatically when the set is created.
Limitations When Creating Sets
The syntax for creating a set using the set()
function has specific requirements. If you attempt to pass multiple arguments directly, such as:
You will encounter a TypeError
, because the set()
function expects a single iterable as its argument, not multiple separate values.
To create a set with multiple elements, you need to provide them as a single iterable:
# Using a list `[]` movies = set(["Tenet", "Dunkirk", "Inception"]) print(movies)
# Using a tuple `(,)` movies = set(("Tenet", "Dunkirk", "Inception")) print(movies)
Swipe to show code editor
Create a set named favorite_movies
containing the titles of at least two of your favorite movies. Print the set to verify its contents.
Thanks for your feedback!
Creating a Set
A set in Python is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate elements, making them ideal for operations like removing duplicates or performing mathematical set operations. Sets are mutable, meaning their elements can be added or removed, but the data inside the set must be immutable (e.g., numbers, strings, or tuples).
Note
A set cannot contain dictionaries or lists because both are mutable data types.
There are two main ways to create a set in Python:
- Using curly braces
{}
with elements separated by commas; - Using the
set()
function.
Here are some key points about sets:
- They are mutable: you can add or remove elements from a set, but the set itself must contain immutable elements;
- Duplicate Elements: if duplicate elements are added to a set, they are automatically removed;
- Unordered: the order of elements in a set is not guaranteed and may vary;
- Diverse Data Types: sets can contain elements of different data types, such as strings, integers, or tuples.
Let's define a set using the set()
function:
# Creating a set which contains strings movie_set = set("Interstellar") print(movie_set)
Next, let's define a set of popular movies using curly braces:
# Creating a set of movies using curly braces movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Tenet"} print(movies)
Duplicate entries like "Tenet"
are removed automatically when the set is created.
Limitations When Creating Sets
The syntax for creating a set using the set()
function has specific requirements. If you attempt to pass multiple arguments directly, such as:
You will encounter a TypeError
, because the set()
function expects a single iterable as its argument, not multiple separate values.
To create a set with multiple elements, you need to provide them as a single iterable:
# Using a list `[]` movies = set(["Tenet", "Dunkirk", "Inception"]) print(movies)
# Using a tuple `(,)` movies = set(("Tenet", "Dunkirk", "Inception")) print(movies)
Swipe to show code editor
Create a set named favorite_movies
containing the titles of at least two of your favorite movies. Print the set to verify its contents.
Thanks for your feedback!