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.
my_dict = {
'name' : 'John' ,
'age' : 25 ,
'city' : 'New York'
} print (my_dict[ 'name' ])
print (my_dict[ 'age' ])
print (my_dict[ 'city' ])
|
John 25 New York
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.
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" )
|
Original 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.
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" )
|
Original 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.
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" )
|
Original Dictionary Size: 248 bytes Optimized Dictionary Size (using array): 136 bytes