Python Set Operations | Union, Update, Constructor With Example

Python Set Operations

/ins>

Python Set Operations 

This is the python set operations module. And as we have mentioned this before in this Python tutorial that set is nothing but a collection that is unindexed and unordered. While written sets it is important for you to remember that these sets are written with curly brackets. For example, if you need to create a particular set then

 

It is also important for you to remember that since the sets are unordered so all the items in a set will appear in a random fashion.

 

To Access Items

Since sets are unordered and unindexed so you cannot use any particular index number to refer to any of these items. However, you can choose to loop through all the items that are present in a particular set. You can loop through the set items by using the for loop. Or you can also choose to specify a particular item to see if it is present in the set or not by using the in keyword or the python set add method. For example, if you wish to loop through the entire set then

Also if you wish to check if the item named “banana” is present in the list or not then you can do that by the following the below-mentioned python set add method

 

To Change Items

It is a widely known fact amongst the individuals who are familiar with this programming language that once you create any particular set then you cannot change any of the items that are present in that particular set. However, you can choose to add new items in any particular set by using the python set union method.

 

To Add Items

If you wish to add a single item to a particular set then it is recommended that you should use the add () method or python set union method. For example,

But if you wish to add a number of items to any particular set then in that scenario it is recommended that you should use the update () method. For example

 

To Get the Length of a Set

While using this programming language, if you have created any particular set and you wish to know the specific length or the number of items that are present in that particular set then it is advised that you should use the len () method. The python set example for this is mentioned below.

 

To Remove Items

There are two different methods that you can use to remove any particular item that is mentioned in a set. And those two methods are the remove () method and the discard () method. For example, if you wish to remove the item names “banana” by using the remove () method then you should follow the below-mentioned steps

 

 

It is also important for you to remember that if you remove any particular item that does not exist in the list by using the remove () method then that will display an error.

An example for a case in which you wish to remove an item named “banana” by using the discard () method is mentioned below

 

In the case of the discard () method, if you try to remove an item that is not present in the list already then that will not raise any particular error.

If you wish to remove the last item that is present in the set that you have created then it is advised that you should use the pop () method. However, by using this method you wouldn’t know which particular item gets removed as sets are unordered and any time can be displayed at the end. The python set example for this is mentioned below.

 

If you wish to empty the set entirely of all the items then you should use the clean () method in that particular case.

For Example

 

The Set () Constructor

As a developer who is using this programming language, it is also important for you to remember that to construct a particular set in Python you would be required to use the set ( ) constructor.

For example


With this, we finish our Python set Operations module of this entire Python Tutorial.