Back to Python
2026-02-017 min read

Create an Empty Set in Python

Learn Create an Empty Set in Python step by step with clear examples and exercises.

Title: Creating an Empty Set in Python - A full guide

Why This Matters

In programming, sets are used to store unique elements in a collection. Sets are particularly useful when we want to perform operations like union, intersection, difference, and membership testing. In Python, sets can be created using various methods, one of which is creating an empty set. This skill is essential for any Python programmer as it forms the foundation for working with sets.

Understanding how to create an empty set in Python is crucial because it allows us to initialize a set that we can later populate with elements. Empty sets are also useful when performing set operations, where having an initial empty set can help avoid errors and make our code more readable.

Prerequisites

Before diving into creating an empty set in Python, you should have a basic understanding of:

  1. Variables and data types in Python
  2. Basic syntax and operators in Python
  3. Understanding the concept of collections (lists, tuples, sets)
  4. Familiarity with Python's built-in functions and constructors
  5. Knowledge of set operations such as union, intersection, difference, and membership testing

Core Concept

In Python, we can create an empty set using the built-in set() constructor or the curly braces {}. Let's explore both methods:

Using the set() constructor

The set() constructor is used to create a new set from any iterable object (like lists, tuples, strings, etc.). When we pass an empty iterable as an argument to the set() constructor, it returns an empty set. Here's an example:

empty_set1 = set() # Creating an empty set using the set() constructor
print("Empty Set 1:", empty_set1) # Output: Empty Set 1: set()

Using curly braces {}

Another way to create an empty set is by using curly braces {}. This method is particularly useful when we want to initialize a set with specific elements right away. However, when we use empty curly braces {}, it also returns an empty set:

empty_set2 = {} # Creating an empty set using curly braces
print("\nEmpty Set 2:", empty_set2) # Output: Empty Set 2: set()

Worked Example

Let's create an empty set and then add elements to it. We will use both methods discussed earlier:

Creating an empty set using the set() constructor

empty_set1 = set()

print("Empty Set 1:", empty_set1) # Output: Empty Set 1: set()

Adding elements to the empty set

empty_set1.add(10)

empty_set1.add(20)

empty_set1.add(30)

print("Empty Set 1 after adding elements:", empty_set1) # Output: Empty Set 1 after adding elements: {10, 20, 30}

Creating an empty set using curly braces

empty_set2 = {}

print("\nEmpty Set 2:", empty_set2) # Output: Empty Set 2: set()

Adding elements to the empty set created with curly braces

empty_set2.add("Apple")

empty_set2.add("Banana")

empty_set2.add("Cherry")

print("Empty Set 2 after adding elements:", empty_set2) # Output: Empty Set 2 after adding elements: {'Apple', 'Banana', 'Cherry'}


In the worked example, we first create an empty set using both methods and verify that they are indeed empty. We then add elements to each set and print them out again to see the changes.

Common Mistakes

  1. Assuming that an empty list [] or an empty dictionary {} can be used as a set. However, they are different data types and behave differently when it comes to set operations.
  2. Forgetting to import the set module before using the set() constructor. This is not necessary in Python as the set module is already imported by default.
  3. Not understanding the difference between a list, tuple, and a set. Remember that sets are unordered collections of unique elements.
  4. Thinking that creating an empty set with curly braces {} will only work when initializing the set with specific elements. In fact, empty curly braces also return an empty set.
  5. Failing to realize that sets do not maintain any order of their elements. This can lead to confusion when working with sets that contain multiple instances of the same element.
  6. Overlooking the need for parentheses when passing a list or tuple as an argument to the set() constructor. For example, set([1, 2, 3]) is correct, while set(1, 2, 3) will raise a TypeError because the arguments are not iterable.

Practice Questions

  1. Write a program to create an empty set using both methods discussed above and store them in variables empty_set1 and empty_set2. Print the sets to verify they are indeed empty.
  2. Given two lists list1 = [1, 2, 3, 4, 5] and list2 = [5, 6, 7, 8, 9], create an empty set that contains the union of both lists.
  3. Create a program to find the intersection between two sets containing the names of students who prefer programming languages Python and Java.
  4. Write a program to verify whether a given number is present in a set or not.
  5. Given a list of numbers numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 5], create an empty set and add the unique elements from the list to it. Print the set to verify that it contains only the unique elements.
  6. Write a program to remove duplicates from a list of strings strings = ["apple", "banana", "cherry", "apple", "orange", "banana", "cherry"] and store the result in a new list.
  7. Given two sets set1 = {1, 2, 3} and set2 = {4, 5}, create an empty set that contains the difference between the two sets (i.e., elements present only in either set1 or set2 but not both).
  8. Write a program to find the symmetric difference between two sets containing the names of students who prefer programming languages Python and Java. The symmetric difference is the set of elements that are present in one set but not the other (i.e., elements that belong either exclusively to Python or Java, but not both).
  9. Create a program to find the union, intersection, and symmetric difference between three sets containing the names of students who prefer programming languages Python, Java, and C++.
  10. Write a program to create an empty set and then populate it with elements read from a file. The file should contain one number per line.

FAQ

  1. Can I create an empty set using a list or dictionary and then convert it into a set?

Yes, you can create an empty list or dictionary and convert it into a set using the set() constructor. However, keep in mind that converting a non-empty list or dictionary to a set will result in a new set containing unique elements from the original collection.

  1. What happens if I try to add duplicate elements to a set?

When you try to add duplicate elements to a set, only one copy of the element is added to the set. The rest are ignored since sets are unordered collections of unique elements.

  1. Is it possible to iterate over a set in Python?

Yes, it is possible to iterate over a set in Python using a for loop. When you iterate over a set, the elements are returned in an arbitrary order. However, since sets are unordered collections, the order may vary each time you run the program.

  1. Why can't I use curly braces {} to create a new dictionary or list with specific keys/values or elements?

In Python, when you use curly braces {}, they are interpreted as either an empty dictionary or an empty set depending on the context. If you want to create a new dictionary or list with specific keys/values or elements, you should use square brackets [] for lists and curly braces {...} for dictionaries.

  1. What is the time complexity of common set operations in Python?

The time complexity of common set operations in Python is as follows:

  • Set creation using the set() constructor or empty curly braces {}: O(n) where n is the length of the iterable (if it's not empty).
  • Adding an element to a set: O(1) on average, but O(n) in the worst case when the set needs to be rehashed.
  • Removing an element from a set: O(1) on average, but O(n) in the worst case when the set needs to be rehashed.
  • Union of two sets: O(m + n) where m and n are the lengths of the two sets.
  • Intersection of two sets: O(m + n) where m and n are the lengths of the two sets.
  • Difference between two sets (set1 - set2): O(m + n) where m and n are the lengths of the two sets.
  • Symmetric difference between two sets (set1 ^ set2): O(m + n) where m and n are the lengths of the two sets.
Create an Empty Set in Python | Python | XQA Learn