Open In App

Time Complexities of Python Dictionary

Last Updated : 02 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python dictionaries provide a fast and efficient way to store and retrieve data using key value pair so understanding the time complexities of dictionary operations is very important for optimizing performance in our programs.

In this article, we will explore the time complexities of various dictionary operations.

Accessing Elements by Key (O(1))

The time complexity of retrieving a value by its key in a dictionary is O(1). This is because dictionaries use a hash table internally which allows constant time lookup.

Example:

Python
# Create a dictionary
dict = {'a': 1, 'b': 2, 'c': 3}

# Access value by key
val = dict['b']
print(val) 

Why O(1)? Hash tables use the hash of the key to find the memory location which enables direct access.

Let's explore the time complexities of other dictionary operations:

Adding or Updating an Element (O(1))

Adding a new key-value pair or updating an existing key is also O(1) in average cases as the dictionary directly places the value in the hash table.

Python
# Add a new key-value pair
dict = {'a': 1, 'b': 2, 'c': 3}
dict['d'] = 4
print(dict)  

# Update an existing key
dict['a'] = 10
print(dict) 


Note: In rare cases where a hash collision occur insertion can take longer due to collision resolution.

Deleting an Element (O(1))

Removing an element by its key also has an average time complexity of O(1) using the same hash table mechanism.

Python
dict = {'a': 1, 'b': 2, 'c': 3}
# Remove a key-value pair
del dict['b']
print(dict) 

Checking if a Key Exists (O(1))

The 'in' keyword allows us to check if a key exists in the dictionary with an average complexity of O(1).

Python
# Check for a key
dict = {'a': 1, 'b': 2, 'c': 3}

print('a' in dict)  
print('b' in dict)  

Iterating Over a Dictionary (O(n))

Iterating over keys, values, or items in a dictionary has a time complexity of O(n) where n is the number of elements.

Python
# Iterate over keys
dict = {'a': 1, 'b': 2, 'c': 3}

for key in dict:
    print(key)

# Iterate over values
for val in dict.values():
    print(val)

# Iterate over items
for key, val in dict.items():
    print(key, val)

Getting All Keys or Values (O(n))

Using the keys() or values() method to retrieve all keys or values takes O(n) time since it involves traversing the dictionary.

Python
# Get all keys
dict = {'a': 1, 'b': 2, 'c': 3}

key = list(dict.keys())
print(key) 

# Get all values
val = list(dict.values())
print(val)  

Copying a Dictionary (O(n))

Copying a dictionary creates a shallow copy and its time complexity is O(n) as each key-value pair is duplicated.

Python
# Create a copy of the dictionary
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = dict.copy()
print(dict2)

Clearing a Dictionary (O(1))

The clear() method removes all elements from the dictionary with a time complexity of O(1).

Python
# Clear the dictionary
dict = {'a': 1, 'b': 2, 'c': 3}

dict.clear()
print(dict)  

Next Article
Practice Tags :

Similar Reads