In this Python tutorial, we will discuss, how to create an empty set in Python. Then also, we will see an example of Python initialize empty set. And finally, we will discuss, how to add, update and delete items from set in Python.
Python Set
Python set is an unordered collection of unique items or elements and it is unindexed. The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is unordered, indexing has no meaning. We cannot access or change an element of a set using indexing or slicing.
Set elements are unique. Duplicate elements are not allowed. A set itself may be modified, but the elements contained in the set must be of an immutable type.
Python Set is created by placing all items inside the curly braces.
Example:
my_sets = {"Apple", "Orange", "Banana"}
print(my_sets)
After writing the above code (python sets), Ones you will print “my_sets” then the output will appear as a “ {‘Orange’, ‘Apple’, ‘Banana’} ”. Here, we can see that sets are unordered so it can be in any random order.
You can refer to the below screenshot for Python Sets
Create an empty set in Python
Python provides two ways to create sets: using the set() function and using curly braces ({ }). However, if you try to create an empty set using curly braces, you’ll end up creating an empty dictionary instead. This is because Python interprets {} as an empty dictionary.
Therefore, the correct way to create an empty set in Python is by using the set() function without any argument. Here’s how:
Example:
# Creating an empty set
empty_set = set()
# Checking the type of the variable
print(type(empty_set))
When you run this code, you’ll get <class 'set'>
as output, confirming that the variable is indeed a set. Check out the output in the below screenshot.
Read: How to delete an element from a Python list
Python Initialize Empty Set
Now, let’s see how you can initialize an empty set in Python and add elements to it. You can add elements using the add() function, which takes one argument, the element you want to add to the set.
Here’s an example:
# Initializing an empty set
my_set = set()
# Adding elements to the set
my_set.add(1)
my_set.add(2)
my_set.add(3)
# Printing the set
print(my_set)
When you run this code, you’ll get {1, 2, 3}
as output.
Adding, Updating, and Deleting Items from an Empty Set in Python
After creating an empty set in Python, you can modify it by adding, updating, or deleting items. Let us check out with a few examples.
Add item to Python Empty Set
You can add an element or item to a set in Python using the add()
method. This method takes one argument: the element you wish to add.
Here’s an example:
# Initializing an empty set
my_set = set()
# Adding elements to the set
my_set.add('Python')
my_set.add('Java')
my_set.add('JavaScript')
# Printing the set
print(my_set)
When you run this code, you’ll get {'Python', 'Java', 'JavaScript'}
as output. Remember, sets are unordered, so the order of the elements when printed can be different from the order in which they were added.
You can see the output like below:
Update a Python Empty Set
The update()
method allows you to add multiple elements to a set at once. It takes an iterable (like a list or another set) as an argument. All elements from the iterable are added to the set, and duplicate elements are ignored.
Here’s how to use the update()
method:
# Initializing an empty set
my_set = set()
# Adding elements to the set using update()
my_set.update(['Ruby', 'C++', 'C#'])
# Printing the set
print(my_set)
Deleting Elements or Items from an Empty Set
Python provides several methods to remove elements from a set:
- remove(): This method removes a specified element from the set. If the element is not found, it raises a KeyError.
# Initializing a set
my_set = {'Ruby', 'C++', 'C#'}
# Removing an element from the set
my_set.remove('Ruby')
# Printing the set
print(my_set)
When you run this code, you’ll get {'C++', 'C#'}
as output.
- discard(): This method also removes a specified element from the set. However, if the element is not found, it does nothing and doesn’t raise an error.
# Initializing a set
my_set = {'Ruby', 'C++', 'C#'}
# Discarding an element from the set
my_set.discard('Ruby')
# Printing the set
print(my_set)
When you run this code, you’ll get {'C++', 'C#'}
as output. If you replace 'Ruby'
with a value that isn’t in the set, the set will remain unchanged, and no error will occur.
- pop(): This method removes and returns an arbitrary element from the set. If the set is empty, it raises a KeyError.
# Initializing a set
my_set = {'Ruby', 'C++', 'C#'}
# Popping an element from the set
popped_element = my_set.pop()
# Printing the popped element and the set
print("Popped Element: ", popped_element)
print("Set after pop: ", my_set)
When you run this code, it will print the popped element and the set without the popped element. The output can vary because the pop()
method removes a random element.
- clear(): This method removes all elements from the set.
# Initializing a set
my_set = {'Ruby', 'C++', 'C#'}
# Clearing the set
my_set.clear()
# Printing the set
print(my_set)
When you run this code, you’ll get {}
as output, indicating that the set is now empty.
Access items in set python
In python, set items cannot be accessed by referring to the index, since sets are unordered and the items have no index. But you can loop through the set items by using for loop.
Example:
my_sets = {"Apple", "Orange", "Banana"}
for a in my_sets:
print(a)
After writing the above code (access items in set python), Ones you will print ” a ” then the output will appear as a “ Banana Orange Apple ”. Here, by using for loop we can access the items from sets.
You can refer to the below screenshot access items in set python.
In this tutorial, we have learned, what is a set in Python, how to create an empty set in Python. Then we discussed, how to initialize an empty set in Python. And finally, we saw with a few examples, how to add, update and delete items from an empty set in Python with examples.
You may also like:
- Python check if a variable is an integer
- Python Modulo Operator
- Python program to print prime numbers
- How to get current directory name in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.