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, 22 November 2016

Python Set & Frozenset - Union, Intersection, Difference, Symmetric Difference

Python Set & Frozenset - This is the 16th article of our tutorial series on Python and we are going to have an introduction to another Python data structure - Sets and Frozensets. Like Python Strings, Lists and Tuples, they are also sequences of Python objects, so having some knowledge of Python Strings, Lists and Tuples will be handy. I recommend you to have a look over our articles on Python Strings, Python Lists and Python Tuples, as it would be very useful when we study Python Sets, their properties and basic operations on them. In this article, we will also cover similarities and dissimilarities between Python Sets and other sequences mentioned above. Let's go!

python-set-frozenset

As already mentioned, Python sets are similar to Python Strings, Lists and Tuples, as they are sequence of Python objects. But, unlike all of them, Python sets are Unordered collections, just like Python Dictionaries, as elements in Python sets are not associated with any positional parameters which we call as offset or index. Like Python Lists and unlike Python Strings and Tuples, Python sets are mutable, which means that, we can add, remove or modify the contents of a Python set. Unlike other Python data structures, Python sets have a unique and important property - every element in a Python set is unique across the sequence. To rephrase this, every element in a Python set can have only one occurrence in that particular set, no multiple occurrences are allowed. Also, another very important thing is that, every element in the Python set is a Python immutable object - which means it may be a str type or an int type, but it cannot be a list type. Too much information..? Don't worry, we will revisit all these properties as we proceed in subsequent portion of this article. For that, we need a Python set and let's create one.

Creating Python Sets

Like we have str(), list(), dict() and tuple() functions, we also have a Python built-in set() function for creating a Python set. All we need to do is that, just provide set() function with a Python iterable as an argument, to get a set of unique elements in that iterable. So, we can create a Python set using a String, a List and a Tuple. Another way of creating a Python set is to use curly braces ({ }. We have used curly braces in Python dictionaries, but a dictionary is a collection of key : value pairs of Python objects, while sets being an unordered sequence. So, we just need to put in some immutable Python objects within curly braces to create a Python set. Lets learn how a set is created using set() function and { }.

# Creating a Set using a Python String
>>> mySet = set('CodeNinjaDotIn')
>>> print mySet
set(['a', 'C', 'e', 'd', 'i', 'j', 'o', 'N', 'I', 't', 'n', 'D'])

# Any Python set is of 'set' type or an instance of class 'set'
>>> type(mySet)
<type 'set'>

# Creating a Set using a Python List
>>> mySet = set(['Orange', 'Mango', 'Pinapple', 101, 777])
>>> print mySet
set(['Orange', 777, 'Mango', 101, 'Pinapple'])

# A Set always have unique elements from an iterable
>>> mySet = set(['Orange', 'Mango', 'Pinapple', 101, 777, 'Mango'])
>>> print mySet
set(['Orange', 777, 'Mango', 101, 'Pinapple'])

# Tuple as an iterable to create a Set
>>> mySet = set(('One', 2, 'Three', 4, 'One', 'Three'))
>>> print mySet
set([2, 4, 'Three', 'One'])

In above example, it is very much clear that, every set belongs to type set or in other words, it is an instance of set class. Another observation is that, we have used an iterable - a String, a List and a Tuple, to create a Python set, and every item from that iterable is an immutable Python object. For example, the alphabets 'C', 'N', etc. are immutable Python objects, while every item in the list and the tuple, is either a string or an integer, again immutable objects. To verify this statement, we try to create a Python set using an iterable containing a mutable Python object - a List may be.

# [2, 3, 4] in the list below, is a mutable object
>>> mySet = set(['One', [2, 3, 4], 'Five'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

No, we cannot. Also, as mentioned earlier, if the iterable contains multiple occurrences of any items, a set created using that iterable will contain only one occurrence of that item. Thus, it is guaranteed that, every element in the list is unique across the set. One more thing, all the elements in the set may not occur in the same order as in the iterable provided, as a set is an Unordered collection. Let us now create s Python set using curly braces.

# Creating a Python Set using '{ }'
>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange', 'Banana', 'Apple'}
>>> print mySet
set(['Orange', 'Mango', 'Apple', 'Banana'])
>>> type(mySet)
<type 'set'>

# We cannot use a mutable object ['Banana', 'Apple'] to create a Set
>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange', ['Banana', 'Apple']}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

So far, we tried to create a set with immutable objects, like integers and strings, in an iterable. We couldn't create one with a list, a mutable object, as a set item. Can we create a set with a tuple, an immutable object, as an item in the set? Lets try this.

# Using '{ }' and tuple as an item
>>> mySet = {'Apple', 'Banana', 'Mango', 'Orange', ('Banana', 'Apple')}
>>> mySet
set(['Orange', 'Mango', ('Banana', 'Apple'), 'Apple', 'Banana'])

# With 'set()' function and tuple as an item
>>> mySet = set(['Apple', 'Banana', 'Mango', 'Orange', ('Banana', 'Apple')])
>>> mySet
set(['Orange', 'Mango', ('Banana', 'Apple'), 'Apple', 'Banana'])

We could create it. Tuples being the immutable objects, can be used as set items.

Sets are Mutable!

Whenever properties of various Python data structures are discussed, we have always talked about mutability of Python objects. Here, in case of sets, we will also discuss whether they are mutable or not. As the heading of the section tells, they are mutable. To verify the same, we make use of add() method of Python sets. This method accepts a Python object, obviously an immutable one, and adds it to the set. Lets check adding 'Pineapple' to our set of fruits.

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

# Lists? No place for them
>>> mySet.add(['Guava', 'Watermelon'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

So, Pythons sets are mutable objects which contain immutable Python objects. Having discussed and proven mutability of sets, its right time to introduce Frozensets to you.

Frozensets

Well, frozensets are exactly like sets, but they are different that them, as far as mutability is concerned - Frozensets are immutable. Before we check this, we need to create a frozenset using Python built-in frozenset() function, which works just like set() function as learned earlier, but returns a frozenset type.

>>> mySet = frozenset(['Apple', 'Banana', 'Mango', 'Orange', 'Apple'])
>>> mySet
frozenset(['Orange', 'Mango', 'Apple', 'Banana'])
>>> type(mySet)
<type 'frozenset'>

# Adding an element to a frozenset
>>> mySet.add('Potato')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

Very simple! Now, we move ahead to know about some more common terms related to sets. These terms are the same as the ones which we studied in Set theory in Mathematics. These are - Union, Intersection, Difference, Symmetric difference and Subsets. Lets learn each of these in the following sections.

Union of Sets

union-of-sets
Union of two sets A and B is another set that contains all the elements in sets A and B. In Python, Union operation can be performed on two Python sets using the operator | or by using the method union(). So, 'A Union B' can be done using the syntax A | B or A.union(B).

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

Intersection of Sets

Intersection of two sets A and B is another set of elements those are present in both sets A and B (common to both of them). In Python, Intersection operation can be performed on two Python sets using the operator & or by using intersection() method. So, 'A Intersection B' can be done using the syntax A & B or A.intersection(B).

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

Difference of Sets

Difference of two sets A and B is a set of elements those are present in one set, but not in other. In Python, Difference of two Python sets can be obtained using the operator - or by using difference() method. So, 'A - B' is the set of elements which are present in A but not in B and this can be obtained using the syntax A - B or A.difference(B).

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

Symmetric Difference of Sets

Symmetric difference of the sets is the set of elements those are not common to both the sets. In Python, we have the operator ^ which gives us the symmetric difference of two sets. We also have a method symmetric_difference() that would also give identical results. So, the syntax for this operation becomes A ^ B or A.symmetric_difference(B).

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

Subset and Superset of a Set

A set B is called a subset of set A if and only if all the elements in set B are present in set A. We can check if a set is a subset of another set using issubset() method. On the other hand, we can also check if a set is a superset of another set using superset(). Both the methods will return True if the condition follows, else False.

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

>>> A.issubset(B)
False
>>> B.issubset(A)
False

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

>>> A.issuperset(B)
False
>>> B.issuperset(A)
True

With this, we end our discussion on Python sets. In this article, I've tried to explain Python sets and their properties in comparison to other Python data structures, so as to have an easier understanding of the sets and revision of other data structures. Meanwhile, we had a short introduction to Frozensets and learned how it is different from Python sets. We have also learned about basic operations those can be performed on Python sets, along with their examples, which also hold good for Python frozensets. In the next article, we will cover various methods associated with Python sets and frozensets. Please share your feedback and opinions in the comment section below and stay tuned. Thank you.

This article is originally published at www.codeninja.in - Python Sets and Frozensets

0 comments:

Post a Comment