Minimizing Dictionary Memory Usage in Python
Last Updated :
11 Mar, 2024
We have a dictionary and we need to Minimizing Dictionary Memory Usage in Python. In this article, we will see some generally used methods for Minimizing Dictionary Memory Usage in Python.
Example:
Input : {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
Output : Original Dictionary Size: 184 bytes
Optimized Dictionary Size : 64 bytes
Explanation : Here, the Original Dictionary Size is 184 bytes and after optimized is 64 bytes.
Minimizing Dictionary Memory Usage in Python
Below are the methods for Minimizing Dictionary Memory Usage in Python:
- Using named tuple
- Using Custom Object
- Using Memory-Efficient Data Structure
Create Basic Dictionary
Below, Python code defines a dictionary named my_dict
with key-value pairs representing personal information.
Python3
my_dict = {
'name': 'John',
'age': 25,
'city': 'New York'
}
print(my_dict['name'])
print(my_dict['age'])
print(my_dict['city'])
Minimizing Dictionary Memory Usage Using a Namedtuple
In this example, in below code to enhance memory efficiency, the dictionary is converted into a namedtuple named `MyTuple`, with its keys serving as field names. The code then measures the size of the optimized namedtuple and prints a comparison of the memory sizes, demonstrating potential memory savings through the use of namedtuples.
Python3
import sys
from collections import namedtuple
original_dict = {
'name': 'John',
'age': 25,
'city': 'New York'
}
original_size = sys.getsizeof(original_dict)
# Convert dictionary to a namedtuple
MyTuple = namedtuple('MyTuple', original_dict.keys())
optimized_dict = MyTuple(**original_dict)
# Measure the size of the optimized dictionary
optimized_size = sys.getsizeof(optimized_dict)
print(f"Original Dictionary Size: {original_size} bytes")
print(f"Optimized Dictionary Size (using namedtuple): {optimized_size} bytes")
OutputOriginal Dictionary Size: 248 bytes
Optimized Dictionary Size (using namedtuple): 80 bytes
Minimizing Dictionary Memory Usage Using Custom Object
In this example, below code uses the `sys` module to measure the memory size of an initial dictionary and then optimizes it by converting it into a custom object called `MyObject`. The memory sizes of the original dictionary and the optimized object are compared and printed.
Python3
import sys
# Original dictionary
original_dict = {
'name': 'John',
'age': 25,
'city': 'New York'
}
original_size = sys.getsizeof(original_dict)
# Convert dictionary to a custom object
class MyObject:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
optimized_obj = MyObject(**original_dict)
optimized_size = sys.getsizeof(optimized_obj)
print(f"Original Dictionary Size: {original_size} bytes")
print(
f"Optimized Dictionary Size (using custom object): {optimized_size} bytes")
OutputOriginal Dictionary Size: 248 bytes
Optimized Dictionary Size (using custom object): 64 bytes
Minimizing Dictionary Memory Usage Using Memory-Efficient Data Structure
In this example, below code uses the `sys` and `array` modules to measure and optimize dictionary memory usage in Python. It starts with an original dictionary, measures its size, converts it to an array, and then measures the optimized array's size. The printed output compares the memory sizes.
Python3
import sys
from array import array
# Original dictionary
original_dict = {
'name': 'John',
'age': 25,
'city': 'New York'
}
# Measure the size of the original dictionary
original_size = sys.getsizeof(original_dict)
# Convert dictionary to an array
optimized_dict = array('B', bytes(str(original_dict.items()), 'utf-8'))
# Measure the size of the optimized dictionary
optimized_size = sys.getsizeof(optimized_dict)
print(f"Original Dictionary Size: {original_size} bytes")
print(f"Optimized Dictionary Size (using array): {optimized_size} bytes")
OutputOriginal Dictionary Size: 248 bytes
Optimized Dictionary Size (using array): 136 bytes
Similar Reads
Python - Dictionary Values Mean Given a dictionary, find the mean of all the values present. Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4} Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean. Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15} Output : 10.0 Explanation : Mean of
4 min read
Python - Value length dictionary Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor
4 min read
Python - Dictionary Values Division Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Letâs discuss division of like key values and ways to solve it in this article. Met
5 min read
Python | Initialize list with empty dictionaries While working with Python, we can have a problem in which we need to initialize a list of a particular size with empty dictionaries. This task has it's utility in web development to store records. Let's discuss certain ways in which this task can be performed. Method #1 : Using {} + "*" operator Thi
5 min read
Python | Split given dictionary in half While working with dictionaries, sometimes we might have a problem in which we need to reduce the space taken by single container and wish to divide the dictionary into 2 halves. Let's discuss certain ways in which this task can be performed. Method #1 : Using items() + len() + list slicing The comb
6 min read
Python | Split given dictionary in half While working with dictionaries, sometimes we might have a problem in which we need to reduce the space taken by single container and wish to divide the dictionary into 2 halves. Let's discuss certain ways in which this task can be performed. Method #1 : Using items() + len() + list slicing The comb
6 min read