Skip to content
Related Articles

Related Articles

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.

  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:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# set of alphabet
set = {'a', 'b', 'c', 'd', 'e'}
print(set)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :