42.Set in Python-creating,modifying and removing elements,set operations


A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).
However, a set itself is mutable. We can add or remove items from it.
Sets can also be used to perform mathematical set operations like union, intersection, symmetric difference, etc.

Creating Python Sets
A set is created by placing all the items (elements) inside curly braces {}, separated by comma, or by using the built-in set() function.
It can have any number of items and they may be of different types (integer, float, tuple, string etc.). But a set cannot have mutable elements like lists, sets or dictionaries as its elements.
Example:
>>> S={1,2,3}
>>> print(S)
{1, 2, 3}
>>> S = {1.0, "Hello", (1, 2, 3)}
>>> print(S)
{1.0, 'Hello', (1, 2, 3)}
set cannot have duplicate
>>> S = {1, 2, 3, 4, 3, 2}
>>> print(S)
{1, 2, 3, 4}
we can make set from a list
>>> l=[1,2,3,4,4]
>>> S=set(l)
>>> print(S)
{1, 2, 3, 4}
set cannot have mutable item
>>> S={1,2,[3,4]}
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    S={1,2,[3,4]}
TypeError: unhashable type: 'list'

Creating an empty set
>>>S=set()
Note 
>>>S={} will create an empty dictionary

Modifying a set in Python

Sets are mutable. However, since they are unordered, indexing has no meaning.
We cannot access or change an element of a set using indexing or slicing. Set data type does not support it.
We can add a single element using the add() method, and multiple elements using the update() method. The update() method can take tuples, lists, strings or other sets as its argument. In all cases, duplicates are avoided.
Example:
>>> S={1,3}
>>> S
{1, 3}
>>> S.add(4)
>>> S
{1, 3, 4}
>>> S.update([2,3,5])
>>> S
{1, 2, 3, 4, 5}
>>> S.update([7,8],{9,10})
>>> S
{1, 2, 3, 4, 5, 7, 8, 9, 10}

Removing elements from a set

A particular item can be removed from a set using the methods discard() and remove().

The only difference between the two is that the discard() function leaves a set unchanged if the element is not present in the set. On the other hand, the remove() function will raise an error in such a condition (if element is not present in the set).

The following example will illustrate this.
>>> S={1,2,3,4}
>>> S.discard(4)
>>> S
{1, 2, 3}
>>> S.remove(3)
>>> S
{1, 2}
>>> S.remove(5)
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    S.remove(5)
KeyError: 5
>>> S.discard(5)

Similarly, we can remove and return an item using the pop() method.
Since set is an unordered data type, there is no way of determining which item will be popped. It is completely arbitrary.
We can also remove all the items from a set using the clear() method.
>>> S={1,5,2,3,4}
>>> S.pop()
1
>>> S.pop()
2
>>> S
{3, 4, 5}
>>> S.clear()
>>> S
set()

Python Set Operations

Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference. We can do this with operators or methods.

Let us consider the following two sets for the following operations.
>>> A = {1, 2, 3, 4, 5} 
>>> B = {4, 5, 6, 7, 8}
Union
Union of A and B is a set of all elements from both sets.
Union is performed using | operator. Same can be accomplished using the union() method.
>>> A|B
{1, 2, 3, 4, 5, 6, 7, 8}
>>> A.union(B)
{1, 2, 3, 4, 5, 6, 7, 8}
>>> B.union(A)
{1, 2, 3, 4, 5, 6, 7, 8}

Intersection
Intersection of A and B is a set of elements that are common in both the sets.
Intersection is performed using & operator. Same can be accomplished using the intersection() method.
>>> A&B
{4, 5}
>>> A.intersection(B)
{4, 5}
>>> B.intersection(A)
{4, 5}

Difference
Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B. Similarly, B - A is a set of elements in B but not in A.
Difference is performed using - operator. Same can be accomplished using the difference() method.
>>> A-B
{1, 2, 3}
>>> B-A
{8, 6, 7}
>>> A.difference(B)
{1, 2, 3}
>>> B.difference(A)
{8, 6, 7}

Symmetric Difference
Symmetric Difference of A and B is a set of elements in A and B but not in both (excluding the intersection).
Symmetric difference is performed using ^ operator. Same can be accomplished using the method symmetric_difference().

>>> A^B
{1, 2, 3, 6, 7, 8}
>>> B^A
{1, 2, 3, 6, 7, 8}
>>> A.symmetric_difference(B)
{1, 2, 3, 6, 7, 8}
 
Subset
The issubset() method returns True if set A is the subset of B, i.e. if all the elements of set A are present in set B . Else, it returns False. 
>>>A = {1, 2, 3} 
>>>B = {1, 2, 3, 4, 5}
# all items of A are present in B 
>>>A.issubset(B)
True
>>>B.issubset(A)
False
Superset

The issuperset() method returns True if a set has every elements of another set (passed as an argument). If not, it returns False.

Set X is said to be the superset of set Y if all elements of Y are in X 
 >>>A= {1, 2, 3, 4, 5}
>>>B = {1, 2, 3}
>>>A.issuperset(B)
True
>>>B.issuperset(A))
False

Disjoint set

The isdisjoint() method returns True if two sets don't have any common items  between them, i.e. they are disjoint. Else the returns False.

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

Comments

Popular posts from this blog

Python For Machine Learning - CST 283 - KTU Minor Notes- Dr Binu V P

KTU Python for machine learning Sample Question Paper and Answer Key Dec 2020

46.Classes and Objects in Python- Accessors and mutators