Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Set | Set
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
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:

  1. Using curly braces {} with elements separated by commas;
  2. 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:

123
# Creating a set which contains strings movie_set = set("Interstellar") print(movie_set)
copy

Next, let's define a set of popular movies using curly braces:

123
# Creating a set of movies using curly braces movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Tenet"} print(movies)
copy

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:

123
# Using a list `[]` movies = set(["Tenet", "Dunkirk", "Inception"]) print(movies)
copy
123
# Using a tuple `(,)` movies = set(("Tenet", "Dunkirk", "Inception")) print(movies)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
toggle bottom row

book
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:

  1. Using curly braces {} with elements separated by commas;
  2. 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:

123
# Creating a set which contains strings movie_set = set("Interstellar") print(movie_set)
copy

Next, let's define a set of popular movies using curly braces:

123
# Creating a set of movies using curly braces movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Tenet"} print(movies)
copy

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:

123
# Using a list `[]` movies = set(["Tenet", "Dunkirk", "Inception"]) print(movies)
copy
123
# Using a tuple `(,)` movies = set(("Tenet", "Dunkirk", "Inception")) print(movies)
copy
Task
test

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt