Home >>Python Tutorial >Python Set Operations

Python Set Operations

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

thisset = {“apple”, “banana”, “cherry”}

print (thisset)

  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

thisset = {“apple”, “banana”, “cherry”}

for x in thisset:

   print (x)

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

thisset = {“apple”, “banana”, “cherry”}

print (“banana” in thisset)

 

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,

thisset = {“apple”, “banana”, “cherry”}

thisset.add (“orange”)

print (thisset)

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

thisset = {“apple”, “banana”, “cherry”}

thisset.update ( [ “orange”, “mango”, “grapes” ] )

print (thisset)

 

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.

thisset = {“apple”, “banana”, “cherry”}

print (len (thisset) )

 

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  

thisset = {“apple”, “banana”, “cherry”}

thisset.remove (“banana”)

print (thisset)

  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  

thisset = {“apple”, “banana”, “cherry”}

thisset.discard (“banana”)

print (thisset)

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.  

thisset = {“apple”, “banana”, “cherry”}

x = thisset.pop ( )

print (x)

print (thisset)

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

thisset = {“apple”, “banana”, “cherry”}

thisset.clear ( )

print (thisset)

If you wish to delete the entire set, then you should use the del keyword in that scenario. For example

thisset = {“apple”, “banana”, “cherry”}

del thisset

print (thisset)

 

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

thisset = set ( (“apple”, “banana”, “cherry”) ) # note the double round brackets

print (thisset)

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


No Sidebar ads