The Python dictionary(Python Document) is a powerful data structure that stores data as key-value pairs and guarantees insertion order since Python 3.7.
For me, it’s one of the most comfortable and versatile data types available. I often use dictionaries more than lists when writing quick, on-the-fly code (though less so in commercial programs). Their flexibility makes them an easy go-to option.
I’ve even used dictionaries temporarily before inserting data into a database. However, I’ve noticed that I sometimes use them unnecessarily when a simpler list or tuple would suffice. To avoid this, I try to consider simpler alternatives first.
Benefits
- Fast data access: Retrieve values using keys with an average time complexity of O(1)O(1).
- Flexibility: Supports storing various data types as values, enhancing versatility.
- Readability: The key-value structure clearly conveys the purpose and meaning of the data.
- Dynamic management: Easily add, delete, or modify elements in the dictionary.
Disadvantages
- Memory usage: Uses more memory than lists due to the underlying hash table structure.
- Key restrictions: Keys must be hashable data types, such as strings, numbers, or tuples containing only hashable elements.
- No reordering: The order of keys follows insertion order but cannot be explicitly changed.
- No duplicate keys: If the same key is defined more than once, only the last value is retained.
Dictionary Code
my_dict = {
"name": "CIL",
"age": 1,
"site": "Blogger"
}
print(my_dict["name"]) # output : CIL
print(my_dict["age"]) # output : 1
# Add Value
my_dict["email"] = "cil@blogger.com"
# Edit Value
my_dict["age"] = 2
# delete Key:value
del my_dict["site"]
print(my_dict) # output : {'name': 'CIL', 'age': 2, 'email': 'cil@blogger.com'}
for key in my_dict:
print(key)
for value in my_dict.values():
print(value)
for key, value in my_dict.items():
print(f"{key}: {value}")
keys = my_dict.keys()
print(keys) # output : dict_keys(['name', 'age', 'email'])
values = my_dict.values()
print(values) # output : dict_values(['CIL', 2, 'cil@blogger.com'])
items = my_dict.items()
print(items)
# output : dict_items([('name', 'CIL'), ('age', 2), ('email', 'cil@blogger.com')])
if "name" in my_dict:
print("Name in dictionary")
major = my_dict.get("major", "Undeclared")
print(major) # output : Undeclared
# default key
graduation_year = my_dict.setdefault("graduation_year", 2024)
print(graduation_year) # output : 2024
print(my_dict)
# output : {'name': 'CIL', 'age': 2, 'email': 'cil@blogger.com',
# 'graduation_year': 2024}
Output
CIL
1
{'name': 'CIL', 'age': 2, 'email': 'cil@blogger.com'}
name
age
email
CIL
2
cil@blogger.com
name: CIL
age: 2
email: cil@blogger.com
dict_keys(['name', 'age', 'email'])
dict_values(['CIL', 2, 'cil@blogger.com'])
dict_items([('name', 'CIL'), ('age', 2), ('email', 'cil@blogger.com')])
Name in dictionary
Undeclared
2024
{'name': 'CIL', 'age': 2, 'email': 'cil@blogger.com', 'graduation_year': 2024}
When discussing dictionaries, it’s often noted that they consume a lot of memory due to their hash table structure.
In my experience, I once needed to analyze the similarity of words in news headlines and considered using Tensors with libraries like PyTorch or TensorFlow.
However, the program became too heavy, so I switched to dictionaries to mimic the functionality. In hindsight, this may not have been the best choice, as the memory usage of dictionaries likely contributed to the program’s heaviness.
There are many advanced techniques for vectorizing words and comparing their similarity, such as using embeddings, normed spaces, or Hilbert spaces. However, diving into those methods might be better suited for a more focused discussion.