Working with Sets in Python
Sets in Python
Set is a collection of data (of different data types) and is specified using curly brackets.
Set is one of the Sequence data types in Python and has unique features compared to the other data types (list, tuple and dictionary).
Below are some of the features of Sets.
- Sets can hold data of different data types.
- Data in the set can only be added or removed and cannot be amended.
- Data in the set is unordered (i.e., every time a program is run, same data is stored in different order) and cannot be accessed by using index.
- Duplicate data is not allowed in Sets.
In this post, we will see how to access, add or remove the data from sets and the use of different set methods.
Creating a set in Python
Before we go on to different methods of a set, let us see How to create a set in Python.
A set is created when the data is assigned with in curly brackets (Unlike dictionaries, no key value would be present in Sets).
Syntax:
new_set = {"Value1", "Value2",...}
As I mentioned in the features, data is stores in no specific order and might vary between each run. Let's try running the above code twice and see how the data in the set is stored.
Result - 1:
Creating a set by copying the data
Other way of creating a set is by copying the data from another set. This can be done by using the method copy(). This method doesn't accept any arguments.
Syntax:
new_set = old_set.copy()
In the above example, A set is created by by copying the data from another set which is already created.
Accessing the data from a Set
Data in the set is neither indexed nor ordered. So, how do we access the data in a set? Specific value from set cannot be accessed as there is no index or key. But, Data in the set can be accessed by iterating in a loop.Let's have a look at the example using for loop to access the data from a set.
In the above example, for loop is executed for each value present in the set. Again, the data in the set is stored in no specific order, so every time the program is run, sequence of the data returned could be different.
Adding the data to a Set
Data present in the set cannot be amended. But, new data can be added by using the method 'add()'. This method accepts a single argument (i.e., data to be added).Syntax:
new_set.add("Data to be added")
Let's have a look at the example.
- Line - 4: We are adding "NEW" to the set. "NEW" is already present, so another entry won't be added in the set. Duplicate values aren't allowed.
- Line - 5: We are adding "NEW DATA" to the set. This would added to the set in no specific order.
If we print a set after adding the new data, Just like creating a set, data wouldn't be in specific order.
add() method is helpful if there is only one element that needs to be added to a set. If we need to add a set of values from another set, update() method is helpful to do this. This method accepts an iterable as an argument and adds each element in the iterable to the set. Data in the set passed as an argument won't be updated.
Syntax:
set_one.update(set_two)
This can be easily understood with below example.
- Line - 7: Passing the set as an argument to update method would add the data to 'set_one'. Any duplicate values will not be added.
- Line - 12: We are passing a string as an argument. Unlike, the add() method string won't be added to a set as is. update() method would consider the string as an iterable and adds each character as separate value in a set.
Deleting the data from a Set
There are multiple methods to delete the data from a set.
- remove()
- discard()
- pop()
remove() method accepts the data that needs to be removed from a set.
Syntax:
set_one.remove("Data to be removed")
Value passed in the argument would be removed from a set. If a value that is not present in the set is passed an exception will be thrown.
Like remove() method, discard() method accepts an argument that needs to be removed from a set.
set_one.remove("Data to be removed")
So, what's the difference between remove() and discard methods()? remove() method throws exception when the data that is not present in the set is passed. And, discard() method would ignore the exception if the data not present in a set is passed.
Syntax:
set_one.pop()
There is one other way to delete the data from a set is by clearing the data from a set using the clear() method. This would clear all the data present in a set.
Syntax:
set_one.clear()
union() and intersection() are the two useful methods when working with two different sets.
union() - accepts a set as an argument and returns the set with the data from both the referring sets (any duplicate data would only present once in the result).
Syntax:
new_set = set_one.union(set_two)
intersection() - accepts a set as an argument and returns the set with the data that is present in both the referring sets.
Syntax:
new_set = set_one.intersection(set_two)
With the use of above union and intersection methods, data from both the sets won't be affected. New set would be returned with the corresponding result.
There is one other method to do intersection is intersection_update(). This doesn't return a resulting set instead updates the calling set with the result. i.e., existing data from the set would be cleared and only common data between both the sets would be updated.
Below example shows the use of all three methods.
- Line - 7: union() method would return the data from both the sets and would be printed by print function. Both sets 'set_one' and 'set_two' won't be updated with this operation.
- Line - 11: intersection() method would return the data that is common to both the sets and printed by print function. Both sets 'set_one' and 'set_two' won't be updated with this operation.
- Line - 16: intersection_update() method would update the 'set_one' with the data that is present in both the sets 'set_one' and 'set_two'.
While working with data, It becomes essential to check the relationship between different sets. Like,
- If a set is sub set of other set
- If a set is super set of other set
- If two sets are disjoint
Below methods are helpful achieve this.
issubset()
This method checks if a set is a subset of the set passed in the argument and returns True if it is a subset and False if not a subset.
Syntax:
set_one.issubset(set_two)
issuperset()
This method checks if a set is a superset of the set passed in the argument and returns True if it is a superset and False if not a superset.
Syntax:
set_one.issuperset(set_two)
isdisjoint()
This method checks if a set is disjoint of the set passed in the argument (i.e., no elements are common between both the sets).
Syntax:
set_one.isdisjoint(set_two)
Below is the simple example using these three methods.
Identifying the difference between two sets becomes essential when working with data. Below methods are helpful to retrieve the difference between the sets.
difference()
Returns the data present in a set and not present in the set passed as an argument. Difference data would be returned as a set. None of the two sets would be updated by using this method.
Syntax:
new_set = set_one.difference(set_two)
difference_update()
Updates a set with the data that is not present in set passed in the arguments. In other words, elements present in both the sets would be removed from the initial set.
This method doesn't return any value.
Syntax:
set_one.difference_update(set_two)
Below is a simple example by using these two methods.
difference() method would only return the data difference that is present in the set the method is associated with and ignores the data present in the set passed in the argument.
symmetric_difference() method would consider both the sets and return the data by removing the common elements. In simple words this is like opposite of intersection.
Syntax:
new_set = set_one.symmetric_difference(set_two)
symmetric_difference_update()
This method doesn't return a set instead updates the associated set with the data from both the sets after removing the common elements.
Syntax:
set_one.symmetric_diiference_update(set_two)
Below is a simple example by using these two methods.
Hope the above details were a bit of help to you in understanding more about Sets in Python.
If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.
Comments
Post a Comment