This is a short Python script demonstrating how to extract select key:value pairs from a dictionary.
We start with the initial, unfiltered kv_pairs
dictionary.
kv_pairs = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8, 'i':9, 'j':10}
Next, we define a list of keys for the key:value pairs that we want to extract.
preferred_keys = ['a', 'c', 'e', 'g', 'i']
Looping through the list generates a new dictionary with only the select key:value pairs that we wanted.
select_kv_pairs = {key:kv_pairs[key] for key in preferred_keys}
The resulting select_kv_pairs
dictionary is shown below.
{'a':1, 'c':3, 'e':5, 'g':7, 'i':9}