Python: Dictionary with Multiple Keys

Python dictionary: Accessing one value with multiple keys

Oliver Lövström
Internet of Technology
3 min readJan 13, 2024

--

Python dictionaries allow us to map unique keys to values. What if we could use multiple keys to access the same value?

Photo by Maria Ziegler on Unsplash

Practical Example

Consider a clinical setting where blood test reference values vary for neonates, pediatric patients, and adults. We may need to retrieve these values by inputting either the patient’s exact age or their age group category. How can we design a Python dictionary to handle this?

Source: Complete Blood Count

Implementation

We will create a custom dictionary class, AgeGroupDict, capable of accepting either an integer representing the patient's age or a string specifying their age group. Here's the implementation:

class AgeGroupDict(dict):
def __getitem__(self, key):
if isinstance(key, int):
if key <= 1:
age_group = 'Neonate'
elif key <= 7:
age_group = 'Pediatric'
elif key > 7:
age_group = 'Adult'
else:
raise KeyError("Age must be positive.")
return super().__getitem__(age_group)
elif isinstance(key, str):
return super().__getitem__(key)
else:
raise KeyError("Invalid input. Please provide an age or age group.")

The method __getitem__ handles the logic for determining the age group based on the key provided.

Creating the Normal Ranges Dictionary

Next, we then instantiate AgeGroupDict with normal blood count ranges:

cbc_normal_ranges = AgeGroupDict({
'Adult': {
'WBC': (3.6, 10.6), # Units: 10^9/L.
'RBC': {'Male': (4.20, 6.00), 'Female': (3.80, 5.20)}, # Units: 10^12/L.
'HGB': {'Male': (135, 180), 'Female': (120, 150)}, # Units: g/L.
'HCT': {'Male': (0.40, 0.54), 'Female': (0.35, 0.49)}, # Units: L/L.
'MCV': (80, 100), # Units: fL.
'MCH': (26, 34), # Units: pg.
'MCHC': (320, 360), # Units: g/L.
'RDW': (11.5, 14.5), # Units: %.
'PLT': (150, 450), # Units: 10^9/L.
'Neutrophils': (1.7, 7.5), # Units: 10^9/L.
'Lymphocytes': (1.0, 3.2), # Units: 10^9/L.
'Monocytes': (0.1, 1.3), # Units: 10^9/L.
'Eosinophils': (0.0, 0.3), # Units: 10^9/L.
'Basophils': (0.0, 0.2), # Units: 10^9/L.
},
'Pediatric': {
'WBC': (5.0, 17.0), # Units: 10^9/L.
'RBC': (4.00, 5.20), # Units: 10^12/L.
'HGB': (102, 152), # Units: g/L.
'HCT': (0.36, 0.46), # Units: L/L.
'MCV': (78, 94), # Units: fL.
'MCH': (23, 31), # Units: pg.
'MCHC': (320, 360), # Units: g/L.
'RDW': (11.5, 14.5), # Units: %.
'PLT': (150, 450), # Units: 10^9/L.
'Neutrophils': (1.5, 11.0), # Units: 10^9/L.
'Lymphocytes': (1.5, 11.1), # Units: 10^9/L.
'Monocytes': (0.1, 1.9), # Units: 10^9/L.
'Eosinophils': (0.0, 0.7), # Units: 10^9/L.
'Basophils': (0.0, 0.3), # Units: 10^9/L.
},
'Neonate': {
'WBC': (9.0, 37.0), # Units: 10^9/L.
'RBC': (4.10, 6.10), # Units: 10^12/L.
'HGB': (165, 215), # Units: g/L.
'HCT': (0.48, 0.68), # Units: L/L.
'MCV': (95, 125), # Units: fL.
'MCH': (30, 42), # Units: pg.
'MCHC': (300, 340), # Units: g/L.
'RDW': 'elevated', # TODO.
'PLT': (150, 450), # Units: 10^9/L.
'Neutrophils': (3.7, 30.0), # Units: 10^9/L.
'Lymphocytes': (1.6, 14.1), # Units: 10^9/L.
'Monocytes': (0.1, 4.4), # Units: 10^9/L.
'Eosinophils': (0.0, 1.5), # Units: 10^9/L.
'Basophils': (0.0, 0.7), # Units: 10^9/L.
}
})

Accessing Values

Now, we can retrieve values from the dictionary using either age or age group:

# Accessing WBC range for a 1-year-old.
$ cbc_normal_ranges[1]["WBC"]
(9.0, 37.0)

# Accessing WBC range for the 'Neonate' group.
$ cbc_normal_ranges["Neonate"]["WBC"]
(9.0, 37.0)

Conclusion

In this tutorial, we showcased how multiple keys can be used to access the same value in a Python dictionary. Thank you for reading!

Further Reading

If you want to learn more about programming and, specifically, Python and Java, see the following Coursera course:

Note: If you use my links to order, I’ll get a small kickback. So, if you’re inclined to order anything, feel free to click above.

--

--