In this Python tutorial, we will discuss how to create an empty set in Python, we will learn about the Sets in Python with examples.
- What is a Python set
- How to create an empty set in python
- How to access items from Python Set
- How to add items to Python Set
- How to update set in python
- Find the length of the set in python
- Remove item from set python
- Python join two sets
Python Set
Python set is an unordered collection of items and it is unindexed. 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
To create an empty set in python we have to use the set() function without any arguments, if we will use empty curly braces ” {} ” then we will get an empty dictionary.
Example:
x = set()
print(type(x))
After writing the above code (create an empty set in python), Ones you will print “type(x)” then the output will appear as a “ <class ‘set’> ”. Here, we can use the set() function to get the empty set in python.
You can refer to the below screenshot create an empty set in python
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.
Add items to Python Set
To add items in Python set, we will use the add() method to add any items in sets.
Example:
my_sets = {"Apple", "Orange", "Banana"}
my_sets.add("Mango")
print(my_sets)
After writing the above code (set add items in python), Ones you will print ” my_sets ” then the output will appear as a “ {‘Banana’, ‘Mango’, ‘Apple‘, ‘Orange’} ”. Here, the add() method will add my item ” mango ” in the set.
You can refer to the below screenshot add items to python set.
Update set in python
In python, to add multiple items in the set we will use the update() method and then we pass the list to update which adds all the items in the set.
Example:
my_sets = {"Apple", "Orange", "Banana"}
my_sets.update(["Guava", "Papaya", "Pear"])
print(my_sets)
After writing the above code (update set in python), Ones you will print ” my_sets ” then the output will appear as a “ {‘Apple’, ‘Banana’, ‘Guava’, ‘Papaya’, ‘Pear’, ‘Orange’} ”. Here, the update() method will add all the items in the set.
You can refer to the below screenshot update set in python.
Length of the set in python
In python, we use len() method to get the length of the items and it returns the number of items present in the set.
Example:
my_sets = {"Apple", "Orange", "Banana"}
print(len(my_sets))
After writing the above code (length of the set in python), Ones you will print ” len(my_sets) ” then the output will appear as a “ 3 ”. Here, the len() method will return the number of items in the set.
You can refer to the below screenshot length of the set in python.
Remove item from set python
In python, we use the remove() method to remove any item from the set. If the specified item is not present in the set then it will give an error.
Example:
my_sets = {"Apple", "Orange", "Banana"}
my_set.remove("Orange")
print(my_set)
After writing the above code (remove the item from set python), Ones you will print “(my_sets)” then the output will appear as a “ {‘Banana’, ‘Apple’} ”. Here, the item ” Orange ” is removed from the set.
You can refer to the below screenshot remove item from set python.
Python join two set
In python, to join two set we will use union() method and it will return a new set with all the items from both the set.
Example:
my_set1 = {"Apple", "Orange", "Banana"}
my_set2 = {2, 4, 6}
my_set3 = my_set1.union(my_set2)
print(my_set3)
After writing the above code (python join two sets), Ones you will print “(my_set3)” then the output will appear as a “ {‘Banana’, 2, ‘Apple’, 4, 6, ‘Orange’} ”. Here, the union() method returns a new set containing all items from both sets.
You can refer to the below screenshot python join two sets.
You may like following Python tutorials:
- Python Keywords with examples
- Python While Loop Example
- String methods in Python with examples
- Python check if the variable is an integer
- Check if a number is a prime Python
- Python convert tuple to list
- What does the percent sign mean in python
- Sorting algorithms in Python
- Priority queue in Python
In this Python tutorial, we learned what is a Python Set, how to create an empty set in Python, how to access items from Python set, how to add items to a Python set, how to update set in python, find the length of the set in python, remove the item from set python and also, python join two sets.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.