Python Operators for Sets and Dictionaries
Last Updated :
11 Oct, 2020
Following article mainly discusses operators used in Sets and Dictionaries. Operators help to combine the existing value to larger syntactic expressions by using various keywords and symbols.
- Sets: Set class is used to represent the collection of elements without duplicates, and without any inherent order to those elements. It is enclosed by the {} and each element is separated by the comma(,) and it is mutable.
Example:
Python3
set = { 'a' , 'b' , 'c' , 'd' , 'e' }
print ( set )
|
Output:
{'c', 'b', 'd', 'e', 'a'}
2. Frozensets: It is an immutable form of a set, used to form a set of the set.
Sets and frozensets support the following operators:
- key in s: It is used to check that the given key is in the set or not.
- key not in s: it returns True if the key is not in the set.
Example:
Python3
s = { 4 , 5 , 8 , 6 , 3 , 2 , 5 }
key = 3
x = key in s
y = key not in s
print (x, y)
|
Output:
True False
- s1 == s2 : To check s1 is equivalent to s2. i.e. all elements of s1 are in s2 and vice versa . it return True if s1 is equivalent to s2.
- s1 != s2 : s1 is not equivalent to s2. i.e. at least one elements of s1 is not in s2. it return True if s1 is not equivalent to s2.
Example:
Python3
s1 = { 't' , 3 , 6 , 5 , 7 , 8 , 4 , 9 }
s2 = { 5 , 7 , 8 , 9 , 't' , 4 , 3 , 6 }
x = s1 = = s2
y = s1 ! = s2
print (x)
print (y)
|
Output:
True
False
Comparison of sets is not lexicographic since sets don’t arrange their element in a proper order. Thus, s1 cannot be greater than or less than s2 and vice versa, rather, they can be subsets or supersets.
- s1 <= s2 : s1 is subset of s2. i.e. all elements of s1 are in s2, returns True if s1 is a subset of s2.
- s1 < s2 : s1 is proper subset of s2. i.e. all elements of s1 are in s2 but all element of s2 are not necessarily in s1, returns True if s1 is a proper subset of s2.
- s1 >= s2 : s1 is superset of s2. i.e. all elements of s2 are in s1, returns True if s1 is a superset of s2.
- s1 > s2 : s1 is proper superset of s2. i.e. all elements of s2 are in s1 but all element of s1 are not necessarily in s2, returns True if s1 is a proper superset of s2.
Example :
Python3
s1 = { 2 , 5 , 3 , 7 , 'c' , 'a' , 8 }
s2 = { 3 , 7 , 8 , 'c' }
w = s1 < = s2
x = s1 < s2
y = s1 > = s2
z = s1 > s2
print (w, x, y, z)
|
Output:
False False True True
- s1 | s2: union of s1 and s2, returns elements of both s1 and s2 without repetition.
- s1 & s2: intersection of s1 and s2, returns elements which are present in both sets.
- s1 − s2: set difference, returns elements which are in s1 but not in s2.
- s1 ˆ s2: the set of elements in precisely one of s1 or s2, returns elements which are in s1 but not in s2 and elements which in s2 but not in s1.
Example:
Python3
s1 = { 2 , 5 , 3 , 7 , 'c' , 'a' , 8 }
s2 = { 3 , 7 , 8 , 'c' , 9 , 11 , 'd' }
w = s1 | s2
x = s1 & s2
y = s1 ^ s2
z = s1 - s2
print (w)
print (x)
print (y)
print (z)
|
Output:
{2, 3, 5, ‘a’, 7, 8, 9, 11, ‘d’, ‘c’}
{8, ‘c’, 3, 7}
{2, 5, 9, ‘d’, 11, ‘a’}
{2, 5, ‘a’}
3. Dictionaries: It is a mapping of different keys to its associated values. We use curly brackets { } to create dictionaries too.
Example:
Python3
d = { 'jupiter' : 'planet' , 'sun' : 'star' }
print (d)
|
Output:
{'jupiter': 'planet', 'sun': 'star'}
Dictionaries support the following operators:
- d[key]: it is used to get the value associated with the given key from the dictionary.
- d[key] = value: it is used to set (or reset) the value associated with the given key from the dictionary.
- del d[key]: It is used to delete the key and its associated value from the dictionary.
Example:
Python3
dict = { 'math' : 45 , 'english' : 60 , 'science' : 65 ,
'computer science' : 70 }
x = dict [ 'science' ]
print (x)
dict [ 'english' ] = 80
print ( dict )
del dict [ 'math' ]
print ( dict )
|
Output:
65
{‘math’: 45, ‘english’: 80, ‘science’: 65, ‘computer science’: 70}
{‘english’: 80, ‘science’: 65, ‘computer science’: 70}
- key in d: It is used to check whether a certain key is in the dictionary or not. also called containment check. It returns a boolean value.
- key not in d: It returns a boolean value, True if the key is not present in the dictionary, also called non-containment check
Example:
Python3
dict = { 'math' : 45 , 'english' : 60 , 'science' : 65 ,
'computer science' : 70 }
x = 'english' in dict
y = 'hindi' not in dict
print (x)
print (y)
|
Output:
True
True
- d1 == d2: it compares key-value pair of both dictionaries, if found, returns True.
- d1 != d2: it compares key-value pair of both dictionaries, if found returns True.
Example:
Python3
d1 = { 'a' : 5 , 'b' : 7 , 'c' : 9 , 'e' : 3 }
d2 = { 'c' : 9 , 'a' : 5 , 'b' : 7 , 'e' : 3 }
x = d1 = = d2
y = d1 ! = d2
print (x)
print (y)
|
Output:
True
False
Dictionaries are similar to sets. they also don’t have any elements in a definite order.
The concept of subset and superset is not applicable on dictionaries. Thus, subset and superset operators are meaningless for it.
Please Login to comment...