jlakes85
jlakes85

Reputation: 57

Most efficient way to extract multiple key/value pairs from a list of dictionaries of dictionaries in Python?

Below is the data I'm starting with. Ideally I would like to capture the 'raw' value of the 'avg', 'low', 'high' and 'numberOfAnalysts' keys from the first dictionary within this list of dictionaries, shown in bold (or captured between double asterisks (**) :

[**{'avg': {'raw': 3.02, 'fmt': '3.02'}, 'low': {'raw': 2.5, 'fmt': '2.5'}, 'high': {'raw': 3.15, 'fmt': '3.15'}, 'yearAgoEps': {'raw': 0.91, 'fmt': '0.91'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': 2.319, 'fmt': '231.90%'}}**, {'avg': {'raw': 2.62, 'fmt': '2.62'}, 'low': {'raw': 2.36, 'fmt': '2.36'}, 'high': {'raw': 3.05, 'fmt': '3.05'}, 'yearAgoEps': {'raw': 2.92, 'fmt': '2.92'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': -0.103, 'fmt': '-10.30%'}}, {'avg': {'raw': 10.14, 'fmt': '10.14'}, 'low': {'raw': 8.87, 'fmt': '8.87'}, 'high': {'raw': 10.68, 'fmt': '10.68'}, 'yearAgoEps': {'raw': 8.51, 'fmt': '8.51'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.192, 'fmt': '19.20%'}}, {'avg': {'raw': 11.67, 'fmt': '11.67'}, 'low': {'raw': 9.39, 'fmt': '9.39'}, 'high': {'raw': 13.08, 'fmt': '13.08'}, 'yearAgoEps': {'raw': 10.14, 'fmt': '10.14'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.15100001, 'fmt': '15.10%'}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}]

I've used the following code to slice out the 'avg' key/value pairs, to start:

dispersion_analyst_data_final = dispersion_analyst_data_list_extract
dispersion_analyst_data_final_list_extract = [sub['avg'] for sub in dispersion_analyst_data_final]
print(str(dispersion_analyst_data_final_list_extract))

resulting in the output below:

[{'raw': 3.02, 'fmt': '3.02'}, {'raw': 2.62, 'fmt': '2.62'}, {'raw': 10.14, 'fmt': '10.14'}, {'raw': 11.67, 'fmt': '11.67'}, {}, {}]

I'm almost certain there is a more efficient way to drill down to the final 'raw' value without multiple discrete steps using sub(). The 'raw' values of the first dictionary of the 'avg', 'low', 'high' and 'numberOfAnalysts' data will ultimately be outputted to a CSV file. Is it possible to concatenate multiple sub() operations into the "extract" object?

The final desired data are the 'raw' values of 'avg', 'low', 'high' and 'numberOfAnalysts' within the first dictionary entry in dispersion_analyst_data_list_extract. Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 783

Answers (1)

DarrylG
DarrylG

Reputation: 17156

You mention--The final desired data are the 'raw' values of 'avg', 'low', 'high' and 'numberOfAnalysts' within the first dictionary entry in dispersion_analyst_data_list_extract.

That can be accomplished with the following.

dispersion_analyst_data_list_extract = [{'avg': {'raw': 3.02, 'fmt': '3.02'}, 'low': {'raw': 2.5, 'fmt': '2.5'}, 'high': {'raw': 3.15, 'fmt': '3.15'}, 'yearAgoEps': {'raw': 0.91, 'fmt': '0.91'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': 2.319, 'fmt': '231.90%'}}, {'avg': {'raw': 2.62, 'fmt': '2.62'}, 'low': {'raw': 2.36, 'fmt': '2.36'}, 'high': {'raw': 3.05, 'fmt': '3.05'}, 'yearAgoEps': {'raw': 2.92, 'fmt': '2.92'}, 'numberOfAnalysts': {'raw': 20, 'fmt': '20', 'longFmt': '20'}, 'growth': {'raw': -0.103, 'fmt': '-10.30%'}}, {'avg': {'raw': 10.14, 'fmt': '10.14'}, 'low': {'raw': 8.87, 'fmt': '8.87'}, 'high': {'raw': 10.68, 'fmt': '10.68'}, 'yearAgoEps': {'raw': 8.51, 'fmt': '8.51'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.192, 'fmt': '19.20%'}}, {'avg': {'raw': 11.67, 'fmt': '11.67'}, 'low': {'raw': 9.39, 'fmt': '9.39'}, 'high': {'raw': 13.08, 'fmt': '13.08'}, 'yearAgoEps': {'raw': 10.14, 'fmt': '10.14'}, 'numberOfAnalysts': {'raw': 26, 'fmt': '26', 'longFmt': '26'}, 'growth': {'raw': 0.15100001, 'fmt': '15.10%'}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}, {'avg': {}, 'low': {}, 'high': {}, 'yearAgoEps': {}, 'numberOfAnalysts': {}, 'growth': {}}]

# dispersion_analyst_data_list_extract[0] is the first dictionary
# we access its desired keys using a list comprehension of the first dictionary
desired_output = [dispersion_analyst_data_list_extract[0][k] for k in ['avg', 'low', 'high',  'numberOfAnalysts']]

print(desired_output)
# Out: [{'raw': 3.02, 'fmt': '3.02'},
        {'raw': 2.5, 'fmt': '2.5'},
        {'raw': 3.15, 'fmt': '3.15'},
        {'raw': 20, 'fmt': '20', 'longFmt': '20'}]

Upvotes: 1

Related Questions