Open In App

Python Operators for Sets and Dictionaries

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.

  1. 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:






# set of alphabet
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:

Example:




s = {4, 5, 8, 6, 3, 2, 5}
key = 3
x = key in # containment check
y = key not in # non-containment check
print(x, y)

Output:

True False

Example:




s1 = {'t', 3, 6, 5, 7, 8, 4, 9}
s2 = {5, 7, 8, 9, 't', 4, 3, 6}
  
# equivalent check
x = s1 == s2
  
# non-equivalent check
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.

Example :




s1 = {2, 5, 3, 7, 'c', 'a', 8}
s2 = {3, 7, 8, 'c'}
  
# subset check
w = s1 <= s2
  
# proper subset check
x = s1 < s2
  
# superset check
y = s1 >= s2
  
# proper superset check
z = s1 > s2
print(w, x, y, z)

Output:

False False True True

Example:




s1 = {2, 5, 3, 7, 'c', 'a', 8}
s2 = {3, 7, 8, 'c', 9, 11, 'd'}
  
# union
w = s1 | s2
  
# intersection
x = s1 & s2
  
# elements which are in s1 but not in s2
# and elements which are in s2 but not in s1
y = s1 ^ s2
  
# set difference
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:




# Example of Dictionary
d = {'jupiter': 'planet', 'sun': 'star'}
print(d)

Output:

{'jupiter': 'planet', 'sun': 'star'}

Dictionaries support the following operators:

Example:




dict = {'math': 45, 'english': 60, 'science': 65
        'computer science': 70}
  
# retrieving value by using key
x = dict['science']
print(x)
  
# reassigning value
dict['english'] = 80
print(dict)
  
# deleting
del dict['math']
print(dict)

Output:

65
{‘math’: 45, ‘english’: 80, ‘science’: 65, ‘computer science’: 70}
{‘english’: 80, ‘science’: 65, ‘computer science’: 70}

Example:




dict = {'math': 45, 'english': 60, 'science': 65,
        'computer science': 70}
  
# containment check
x = 'english' in dict
  
# non-containment check
y = 'hindi' not in dict
print(x)
print(y)

Output:

True
True

Example:




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.


Article Tags :