Open In App

Python Operators for Sets and Dictionaries

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Operators are used to perform operations in Python. Let's explore the different operators available for sets and dictionaries and how they can be used for efficient operations.

Operators for sets and frozensets

Sets and frozensets support various operators for common set operations. Sets are mutable, allowing in-place changes, while frozensets are immutable but still support standard set operations. Let's explore the different types of operators.

1. Membership operators

These operators for sets are used to check if an element is present in a set or frozenset. They are particularly fast because sets in Python are built using hash tables, which allow quick lookups.

  • key in s returns True if the key exists in the set.
  • key not in s returns True if the key does not exist in the set.

Example:

Python
s = {1, 2, 3, 4}
print(3 in s)       
print(5 not in s)    

fs = frozenset([1, 2, 3])
print(2 in fs)       

Output
True
True
True

Explanation:

  • print(3 in s) checks if 3 is in the set s and returns True because it is present.
  • print(5 not in s) checks if 5 is not in the set s and returns True because it is absent.
  • print(2 in fs) checks if 2 is in the frozenset fs and returns True because it is present.

2. Equality and Inequality

These operators help you check whether two sets or frozensets are equal or different.

  • s1 == s2 returns True if both sets contain the same elements.
  • s1 != s2 returns True if the sets differ.
Python
s1 = {1, 2, 3}
s2 = {3, 2, 1}
print(s1 == s2)     
print(s1 != s2)    

fs1 = frozenset([1, 2])
fs2 = frozenset([2, 1])
print(fs1 == fs2)    

Output
True
False
True

Explanation:

  • print(s1 == s2) checks if the sets s1 and s2 are equal. Since they contain the same elements, it returns True.
  • print(s1 != s2) checks if the sets s1 and s2 are not equal. Since they are equal, it returns False.
  • print(fs1 == fs2) checks if frozenset fs1 and fs2 are equal. As frozensets are unordered, it returns True.

3. Subset and Superset Operators

These operators let you compare sets in terms of their containment relationships.

  • s1 <= s2 (Subset): All elements of s1 are in s2 and s1 may equal s2.
  • s1 < s2 (Proper subset): All elements of s1 are in s2 and s1 is not equal to s2.
  • s1 >= s2 (Superset): All elements of s2 are in s1 and s1 may equal s2.
  • s1 > s2 (Proper superset): All elements of s2 are in s1 and s1 is not equal to s2.

Example:

Python
s1 = {1, 2, 3, 4}
s2 = {2, 3}

print(s2 <= s1)  
print(s2 < s1)   
print(s1 >= s2)  
print(s1 > s2)   

Output
True
True
True
True

Explanation:

  • print(s2 <= s1) checks if s2 is a subset of s1.
  • print(s2 < s1) checks if s2 is a proper subset of s1.
  • print(s1 >= s2) checks if s1 is a superset of s2.
  • print(s1 > s2) checks if s1 is a proper superset of s2.

4. Set operations

These operators let you perform classic set theory operations like union, intersection and difference. They work the same with frozenset.

Operation

Symbol

Description

Union

|

Combines all elements from both sets

Intersection

&

Elements common to both sets.

Difference

-

Elements in the first set but not the second.

Symmetric Diff

^

Elements in either set, but not both.

Example:

Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

print(a | b)  
print(a & b)  
print(a - b)  
print(a ^ b)   

fs1 = frozenset([1, 2, 3])
fs2 = frozenset([3, 4])
print(fs1 & fs2)  

Output
{1, 2, 3, 4, 5, 6}
{3, 4}
{1, 2}
{1, 2, 5, 6}
frozenset({3})

Explanation:

  • a | b the union of a and b, combining all unique elements.
  • a & b the intersection of a and b, showing common elements.
  • a - b the difference of a and b, elements in a but not in b.
  • a ^ b the symmetric difference, elements in either a or b but not both.
  • fs1 & fs2 the intersection of fs1 and fs2, showing the common element.

Operators for dictionaries

Dictionaries are mutable collections of key-value pairs that support efficient access and manipulation using various operators. These operators help with retrieving, updating, comparing and checking the existence of keys. Let’s explore them.

1. Access and assignment

These are used to get, set, or delete values in a dictionary.

  • d[key] returns the value for the given key.
  • d[key] = value assigns or updates the key's value.
  • del d[key] deletes the key and its value.
Python
d = {"math": 90, "science": 80}
print(d["math"])    

d["science"] = 85
print(d)                 

del d["math"]
print(d)

Output
90
{'math': 90, 'science': 85}
{'science': 85}

Explanation:

  • d["math"] retrieves the value associated with the key "math" from the dictionary d.
  • d["science"] = 85 updates the value of the key "science" to 85.
  • del d["math"] deletes the key "math" and its associated value from the dictionary d.

2. Membership operators

These operators check whether a key is present in the dictionary.

  • key in d checks if a key exists in the dictionary.
  • key not in d checks if a key does not exist in the dictionary.
Python
d = {"name": "Vishakshi", "age": 21}

print("name" in d)       
print("email" not in d)  

Output
True
True

Explanation:

  • "name" in d checks if the key "name" exists in the dictionary d.
  • "email" not in d checks if the key "email" does not exist in the dictionary d.

3. Equality and inequality

These compare dictionaries based on their key-value pairs.

  • d1 == d2 returns True if both dictionaries have the same key-value pairs.
  • d1 != d2 returns True if they differ.
Python
d1 = {"a": 1, "b": 2}
d2 = {"b": 2, "a": 1}

print(d1 == d2) 
print(d1 != d2)

Output
True
False

Explanation:

  • d1 == d2 True, both dictionaries have the same key-value pairs in any order.
  • d1 != d2 False, the dictionaries are identical.

Similar Reads