The markdown version of this document is here.
You can pip-install python_subdict in your environment by typing the following code on your shell:
pip install subdict
As an example, let's say that we have the following dict:
In [1]:
d = {
'a': 'A',
'b': 'B',
'c': 'C',
'd': {
'x': 'D_X',
'y': 'D_Y',
'z': {
'I': 'D_Z_I',
'II': {
'1': 'D_Z_II_1',
'2': 'D_Z_II_2'
},
'III': 'D_Z_III'
}
}
}
If we need only the keys 'a' and 'd', we can do this:
In [2]:
from subdict import extract_subdict # The main function of the library
In [3]:
from pprint import pprint # Just for a nice presentation here
In [4]:
pprint( extract_subdict(d, ['a', 'd']) )
We can also specify 'subkeys' by using a dotted-syntax:
In [5]:
pprint( extract_subdict(d, ['a', 'd.x', 'd.z']) )
The dotted-syntax can have any needed level of depth:
In [6]:
pprint( extract_subdict(d, ['a', 'd.z.II.1']) )
Let's consider the following dict from now on:
In [7]:
person = {
'name': 'John Frusciante',
'birth': '1970-03-05',
'city': {
'name': 'New York City',
'state': {'name': 'New York', 'country': 'USA'}
},
'albums': [
{
'year': 2001,
'name': 'To Record Only Water For Ten Days',
'label': {
'name': 'Warner Bros Records',
'link': 'https://en.wikipedia.org/wiki/Warner_Bros._Records'
}
},
{
'year': 2004,
'name': 'Shadows Collide With People',
'label': {
'name': 'Warner Bros Records',
'link': 'https://en.wikipedia.org/wiki/Warner_Bros._Records'
}
},
{
'year': 2009,
'name': 'The Empyrean',
'label': {
'name': 'Record Collection',
'link': 'https://en.wikipedia.org/wiki/Record_Collection'
}
}
]
}
By default, invalid keys passed to the extract_subdict function are ignored:
In [8]:
extract_subdict(person, ['name', 'birth', 'hair_color']) # 'hair_color' is invalid
Out[8]:
However, by passing True to the strict parameter of the function, invalid keys will raise a KeyError exception:
In [9]:
extract_subdict(person, ['name', 'birth', 'hair_color'], strict=True)
Extracting only 'name' and 'albums' from the person dict:
In [10]:
subdict = extract_subdict(person, ['name', 'albums'])
In [11]:
pprint(subdict)
Now, extracting only the 'name' of each album:
In [12]:
for index in range(len(subdict['albums'])):
subdict['albums'][index] = extract_subdict(subdict['albums'][index], ['name'])
The result is the following:
In [13]:
pprint(subdict)