In this post, you’ll learn sets in Python. And how to create, adding or removing elements from sets in python. We will take examples with python in-built functions on sets.
Sets in python
The set list created in Python is very simple. You will learn everything about Python sets here. In order to understand the end better, we also have many examples with Python sets and with in-built Python methods.
What is a set in Python?
In Python programming, a set is an unordered item collection list. And every item in this list is unique. Also, this list is immutable. The sets are defined by having values between curly brackets { }.
The meaning of immutable is that the list cannot change the item.
However, the sets itself is mutable. We can add or remove items from it.
Python create sets
We have explained about lists, and tuple in back python tutorials. These are different from both Python sets. Now we will know how to create sets in Python.
Let’s take an example. In which we will create a python sets:
mysets = {1, 2, 3, 4}
print(mysets)
Output
{1, 2, 3, 4}
Access sets in python
So far we have learned how to make sets in Python. But now we will know how to access the sets in Python.
To access the sets in Python we can use the for a loop.
Now, we will create a new set and access it with the help of for loop:
mysets = {1, 2, 3, 4}
for a in mysets:
print(a)
Output
1 2 3 4
Python change sets
As we already mentioned python sets are immutable. So once you have created the Python sets, you cannot change its items. But it can add and remove items.
Python in-built methods
Now we will tell you about some Python in-built methods that you can use with Python sets.
You can do add, update, remove, etc. to Python sets with these methods.
Add item to set python
Let us now show you how to add a single item in Python sets.
To add a single item to the Python sets, we use the Python add () method. This method adds a single item/element to the Python sets.
You can see its example below:
mysets = {1, 2, 3, 4}
mysets.add(5)
print(mysets)
Output
{1, 2, 3, 4, 5}
Add multiple items to set python
Let us now show you how to add a multiple items in Python sets.
To add multiple items to the Python sets, we use the Python update() method. This method adds a multiple item/element to the Python sets.
You can see its example below:
mysets = {1, 2, 3, 4}
mysets.update([5, 6, 7])
print(mysets)
Output
{1, 2, 3, 4, 5, 6, 7}
Find length of set in python
If you have any sets and want to remove their length. So you have to use the len () function of Python.
Python’s len() function also finds the length of sets, lists, tuples, etc.
Python: Find the length of a set example:
mysets = {1, 2, 3, 4}
mysets.update([5, 6, 7])
print(len(mysets))
Output
7
Remove item from set python
Python has many methods to remove items from sets such as remove (), discard () method etc. By using these Python built-in methods, items from Python sets can be removed.
There is a method in which an error throws if the item is not found in the sets list. And a method in which there is no error throw if the item is not found in Python sets.
Remove item from set python using remove()
mysets = {1, 2, 3, 4}
mysets.remove(2)
print(mysets)
Output
{1, 3, 4}
Note: If the item to remove does not exist, remove()
will raise an error.
Remove item from set python using discard()
mysets = {1, 2, 3, 4}
mysets.discard(2)
print(mysets)
Output
{1, 3, 4}
Note: If the item to remove does not exist, discard()
will NOT raise an error.
Python Set Methods
We have given a list of built-in python methods below. There are many methods on this list. Which you can use with Python sets.
The built-in Python methods are given in this list. Some of these methods have examples with Python sets above.
Method | Description |
---|---|
add() | Adds an element to the set |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing the difference between two or more sets |
difference_update() | Removes the items in this set that are also included in another, specified set |
discard() | Remove the specified item |
intersection() | Returns a set, that is the intersection of two other sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have a intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with the union of this set and others |