Rules, constraints, properties
- Keys must be unique
- Keys must be hashable (immutable): strings, integers, floats, booleans, tuples
- Dicts maintain the precise insertion order of their keys
- Highly efficient O(1) average time complexity for lookups, insertions, deletions
Search in a dictionary
Return the value associated with the specified key.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
my_dict["make"] # toyota
my_dict["trim"] # raises KeyError since "trim" is not in dict
my_dict.get("trim") # None
my_dict.get("trim", "turbo") # turbo (default value)
Add and delete values in a dictionary
Add a new key pair.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
my_dict["trim"] = "turbo" # {"make": "toyota", "model": "supra", "year": 1997, "trim": "turbo"}
Update a value.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
my_dict["year"] = 1996 # my_dict = {"make": "toyota", "model": "supra", "year": 1996}
Delete a key/value pair.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
del my_dict["make"] # {"model": "supra", "year": 1997}
Delete all key/value pairs.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
my_dict.clear() # {}
Looping through dictionaries
Iterate over key/value pairs.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
for key, value in my_dict.items():
print(f"The value of {key} is {value}")
Retrieve an iterable list of keys or values.
my_dict = {"make": "toyota", "model": "supra", "year": 1997}
my_dict.keys() # dict_keys(['make', 'model', 'year'])
my_dict.values() # dict_values(['toyota', 'supra', 1997])
# Return a list of keys (not a dict_keys instance):
list(my_dict) # ['make', 'model', 'year']