ericmark26 ericmark26 - 1 year ago 73
JSON Question

Parsing sub-properties from a list of dicts in Python

In Python (2.7), I am parsing a list of dictionaries (that come from JSON) where each dictionary has the following format (I'm using fake key names):

{'k': {'p': value_i_care_about_1, 'q': value_i_care_about_2, ... (stuff I don't want)}, ... (stuff I don't want)}

represents key-value pairs I don't care about (do not want).

I need to parse this into a list of tuples where each tuple has the following format:

(value_i_care_about_1, value_i_care_about_2)

If this were Mathematica for instance, I could iterate over the list of dictionaries with
(doesn't really matter) and for each dictionary extract the two keys I want into a list with this code (where
represents the list of dicts):

#[['k', {'p', 'q'}]]& /@ dicts
(in Mathematica you use
for indexing because
is used for function calls).

As of now, in Python I am using the following code

[(d['k']['p'], d['k']['q'])) for d in dicts]

However, in addition to violating DRY a little bit (I'm a bit of a DRY nut), with the full key names, this goes way over PEP 8's line limit of 79 characters. I'm just wondering if there is a better way to go about this.


EDIT: Please see the line under the format of the dictionaries about

Answer Source

I know this is longer, but it has the benefit of being relatively legible and simply extendable.

# you can add any combination of fields here
fields = [['k', 'p'], 
          ['k', 'q']]
result = [tuple(d[level1][level2] for level1, level2 in fields)
          for d in dicts]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download