Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Tuesday, 29 November 2016

Python Set and Frozenset Methods

Python Set and Frozenset Methods - In this article we would learn methods related to Python Sets. Please take a look at our article on Python Sets and Frozensets, in case you missed it. We have already learned some important set methods in the last article, viz. union(), intersection(), difference() and symmetric_difference(), with which we performed basic operations on the sets. In this article, we are going to learn some more methods, their syntax and their examples.

python-set-frozenset-methods

To begin with we need to create a Python set, either using set() function or using { }. With the use of Python built-in function dir(), we can have a list of all the attributes associated with a Python set. For those who do not know about dir() function, it is a function that accepts a Python object as an argument and returns a list type of all the attributes associated with that object. Lets check what all attributes are there with Python sets.

>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange'}
>>> dir(mySet)
['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']

Above is the list of attributes among which some attributes have names starting and ending with double underscores. These attributes are known as Magic Attributes and these are used by Python internally. We do not discuss these attributes for now and see other ones. Please note that, Python frozensets do not have all the methods mentioned above associated with them, they have only a few of them which are - copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference(), union(), which work in exactly the same way they work in case of Python sets. So, we concentrate more on Set methods here and learn them all one-by-one.

1. add() Method :

We have seen and used this method in the last article while knowing whether Python sets are mutable or not. As the name of the method suggests, it adds elements to a Python set. A Python set being a collection of unique objects, if the object to be added is already present in the set, it won't get added to the set. The syntax to use this method is - setName.add(item), where item, as studied in last article, must be a immutable Python object.

>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange'}
>>> mySet.add('Watermelon')
>>> mySet
set(['Orange', 'Mango', 'Watermelon', 'Banana', 'Apple'])

>>> mySet.add('Banana')
>>> mySet
set(['Orange', 'Mango', 'Watermelon', 'Banana', 'Apple'])

2. clear() Method :

The name of this method is also self-explanatory. This method when called on a set, clears all the items to give us an empty set.

>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange'}
>>> mySet
set(['Orange', 'Mango', 'Banana', 'Apple'])
>>> mySet.clear()
>>> mySet
set([])

3. copy() Method :

This method when called on a set, returns a shallow copy of that set. This method can be used to create a copy of the set.

>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange'}
>>> mySet.copy()
set(['Orange', 'Mango', 'Banana', 'Apple'])
>>> anotherSet = mySet.copy()
>>> mySet
set(['Orange', 'Mango', 'Banana', 'Apple'])
>>> anotherSet
set(['Orange', 'Mango', 'Banana', 'Apple'])

4. difference() Method :

This method when called on a set A, accepts an argument, another set B, and returns a set which contains those elements which are present in A but not present B. When no argument is provided, an empty set is considered as an argument and the set A is returned.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}

# Items in A but not in B
>>> A.difference(B)
set([1, 2])

# Items in B but not in A
>>> B.difference(A)
set([8, 6, 7])

# No argument provided -> Original set
>>> A.difference()
set([1, 2, 3, 4, 5])

5. difference_update() Method :

Well, it might be very interesting to know about this method. When we use the difference() method, contents of the sets A and B are not altered. But, when we use difference_update() method, contents of set A are changed, considering that, method is called on set A. So, the set A will contain the result, which is the difference between set A and B. In mathematical term, set A would be - A = A - B.
>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.difference_update(B)
>>> A
set([1, 2])
>>> B
set([3, 4, 5, 6, 7, 8])

6. discard() Method :

This method accepts an argument item and removes it from the set if present, otherwise it does nothing.

>>> A = {1, 2, 3, 4, 5}
>>> A.discard(3)
>>> A
set([1, 2, 4, 5])

>>> A.discard(6)
>>> A
set([1, 2, 4, 5])

7. intersection() Method :

As we've already seen in introductory article on Python sets and frozensets, Intersection of two sets is the set of common elements present in both the sets. In order to get intersection of two sets A and B, we can use intersection() method as A.intersection(B) or B.intersection(A).

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.intersection(B)
set([3, 4, 5])
>>> B.intersection(A)
set([3, 4, 5])

8. intersection_update() Method :

This method when called on some set A with set B as an argument, the result is the intersection of both the sets and result in saved in the set on which method is called i.e. set A. Mathematically we can put this as : A = A & B, where & stands for Intersection.

# Calling 'intersection_update()' method on set 'A', will save result in set 'A'
>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.intersection_update(B)
>>> A
set([3, 4, 5])
>>> B
set([3, 4, 5, 6, 7, 8])

# Calling 'intersection_update()' method on set 'B', will save result in set 'B'
>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> B.intersection_update(A)
>>> A
set([1, 2, 3, 4, 5])
>>> B
set([3, 4, 5])

9. isdisjoint() Method :

In set theory, a set A is a 'disjoint set' of another set B, if there are no common elements in them i.e. their intersection is an empty set. With this method, we can check if a set is a disjoint set of any other set. It will return True if condition is true, otherwise False.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.isdisjoint(B)
False

>>> A = {1, 2}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.isdisjoint(B)
True
>>> B.isdisjoint(A)
True

10. issubset() Method :

In set theory, when all the elements in some set A are present in some other set B, we can say that set A is the subset of set B. With this method, we can check whether a set is a subset of any other set. It will return a boolean type True if the condition is true, else False.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.issubset(B)
False
>>> B.issubset(A)
False

>>> A = {3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.issubset(B)
True
>>> B.issubset(A)
False

11. issuperset() Method :

In set theory, when all the elements in some set A are present in some other set B, we can say that set B is the superset of set A. With this method, we can check whether a set is a superset of any other set. It will return a bool type True if the condition is true, else False.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.issuperset(B)
False
>>> B.issuperset(A)
False

>>> A = {3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.issuperset(B)
False
>>> B.issuperset(A)
True

12. pop() Method :

We have studied pop() method in case of Python lists and it would take an index as an argument and return the element at that index from the list, also removing that element from the list. Here, Python sets are not ordered collection of data, thus we do not have indices associated with set elements, but we have pop() method. So, in case of sets, pop() method will return a random (or arbitrary) element from the set, also removing it from the set. It the set is empty, it will throw a KeyError exception.

>>> A = {1, 2, 3, 4, 5}
>>> A.pop()
1
>>> A
set([2, 3, 4, 5])
>>> A.pop()
2
>>> A
set([3, 4, 5])
>>> A.pop()
3
>>> A
set([4, 5])
>>> A.pop()
4
>>> A
set([5])
>>> A.pop()
5
>>> A
set([])
>>> A.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'

13. remove() Method :

With pop(), an arbitrary element is removed from the set, while with remove() method, we can remove a particular set item, by providing the item to be removed as an argument to the remove() method. Please note that, the item will only be removed if it is present in the set, otherwise it will raise a KeyError exception.

>>> A = {1, 2, 3, 4, 5}
>>> A.remove(3)
>>> A
set([1, 2, 4, 5])
>>> A.remove(4)
>>> A
set([1, 2, 5])
>>> A.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 7

14. remove() Method :

With pop(), an arbitrary element is removed from the set, while with remove() method, we can remove a particular set item, by providing the item to be removed as an argument to the remove() method. Please note that, the item will only be removed if it is present in the set, otherwise it will raise a KeyError exception.

>>> A = {1, 2, 3, 4, 5}
>>> A.remove(3)
>>> A
set([1, 2, 4, 5])
>>> A.remove(4)
>>> A
set([1, 2, 5])
>>> A.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 7

15. symmetric_difference() Method :

In our previous article on Python sets and frozensets, we've learned about Symmetric Difference of two sets. It is the set of elements which are not common to both the sets, exactly opposite to Intersection. When we call this method on a set A and provide set B as an argument, it will return a set of elements which are not common to both sets A and B. We represent this operation using ^ symbol as A ^ B.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.symmetric_difference(B)
set([1, 2, 6, 7, 8])
>>> B.symmetric_difference(A)
set([1, 2, 6, 7, 8])

16. symmetric_difference_update() Method :

When this method is called on a set A with another set B as an argument, the symmetric difference of both the sets is calculated and stored in set A, while set B remains unchanged. We can put this operation as : A = A ^ B

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.symmetric_difference_update(B)
>>> A
set([1, 2, 6, 7, 8])
>>> B
set([3, 4, 5, 6, 7, 8])
>>> B.symmetric_difference_update(A)
>>> A
set([1, 2, 6, 7, 8])
>>> B
set([1, 2, 3, 4, 5])

17. union() Method :

In set theory, Union of two sets is the set of all the elements present in both the sets. This method when called on a set A with set B as an argument, it returns a set which is union of both the sets A and B. The union operation can be represented by the symbol |. So, A | B is the union of two sets A and B.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.union(B)
set([1, 2, 3, 4, 5, 6, 7, 8])
>>> B.union(A)
set([1, 2, 3, 4, 5, 6, 7, 8])

18. update() Method :

When this method is called on a set A and provide another set B as an argument to this method, we get union of these sets stored in A, while set B remains unchanged. We can put this operation as A = A | B, where | represents Union operation.

>>> A = {1, 2, 3, 4, 5}
>>> B = {3, 4, 5, 6, 7, 8}
>>> A.update(B)
>>> A
set([1, 2, 3, 4, 5, 6, 7, 8])
>>> B
set([3, 4, 5, 6, 7, 8])

With this, we end our discussion on Python Set and Frozenset methods. As already mentioned, set of all the methods associated with Python frozensets is subset of the set of methods associated with Python sets. Frozenset methods work exactly the same way as in case of Sets, so we have not covered them explicitly, so please consider this.
Also, we have reached to the end of our discussion on data structures in Python, having covered Python Strings, Python Lists, Python Tuples, Python Dictionaries and Python File Objects in earlier articles. I urge you to please have a quick read on each of them, as it is necessary to know them and their properties in order to understand further part of this tutorial series, as we will be covering the programming part. Please let us know about your feedback and opinions in comment section below and stay tuned for more articles. Thank you.

This article is originally published at www.codeninja.in - Python Set and Frozenset Methods

1 comment:

  1. Very informative. Rich on content article.

    ReplyDelete