0

I have the following list after querying my DB which I'd like to turn into a dictionary:

[{date: date_value1.1, rate: rate_value1.1, source: source_name1},
{date: date_value1.2, rate: rate_value1.2, source: source_name1},
{date: date_value2.1, rate: rate_value2.1, source: source_name2},
{date: date_value2.2, rate: rate_value2.2, source: source_name2},
{date: date_valuenx, rate: rate_valuex, source: source_namex}, ...]

The dictionary should follow the following format:

{
  source_name1:
    [
      {date: date_value1.1, rate: rate_value1.1}
      {date: date_value1.2, rate: rate_value1.2}
    ],
  source_name2:
    [
      {date: date_value2.1, rate: rate_value2.1}
      {date: date_value2.2, rate: rate_value2.2}
    ],
}

I have tried a lot of different code variations, but could not get it to work. What would be the most efficient way to transform the data into the required format?

(This format is the response the client will receive after calling my API. If you have suggestions for better formatting of this response I am also open to suggestions!)

0

2 Answers 2

2

We can use defaultdict and simply append each result to our output list.

from collections import defaultdict

output = defaultdict(list)

data = [{'date': 'date_value1.1', 'rate': 'rate_value1.1', 'source': 'source_name1'}, {'date': 'date_value1.2', 'rate': 'rate_value1.2', 'source': 'source_name1'}, {'date': 'date_value2.1', 'rate': 'rate_value2.1', 'source': 'source_name2'}, {'date': 'date_value2.2', 'rate': 'rate_value2.2', 'source': 'source_name2'}, {'date': 'date_valuenx', 'rate': 'rate_valuex', 'source': 'source_namex'}]

for row in data:
    output[row['source']].append({k: v for k, v in row.items() if k != 'source'})

dict(output)
#{'source_name1': [{'date': 'date_value1.1', 'rate': 'rate_value1.1'}, {'date': 'date_value1.2', 'rate': 'rate_value1.2'}], 'source_name2': [{'date': 'date_value2.1', 'rate': 'rate_value2.1'}, {'date': 'date_value2.2', 'rate': 'rate_value2.2'}], 'source_namex': [{'date': 'date_valuenx', 'rate': 'rate_valuex'}]}

import pprint

pprint.pprint(dict(output))

{'source_name1': [{'date': 'date_value1.1', 'rate': 'rate_value1.1'},
                  {'date': 'date_value1.2', 'rate': 'rate_value1.2'}],
 'source_name2': [{'date': 'date_value2.1', 'rate': 'rate_value2.1'},
                  {'date': 'date_value2.2', 'rate': 'rate_value2.2'}],
 'source_namex': [{'date': 'date_valuenx', 'rate': 'rate_valuex'}]}
Sign up to request clarification or add additional context in comments.

3 Comments

Match his expected output with yours first.
@edusanketdk The output matches for his example.
This achieved exactly what I was looking for. Thank you!
1

try this:

d = [{"date":" date_value1.1", "rate": "rate_value1.1", "source": "source_name1"},
{"date": "date_value1.2", "rate": "rate_value1.2", "source": "source_name1"},
{"date": "date_value2.1", "rate": "rate_value2.1", "source": "source_name2"},]
    
d1 = {}
for ele in d:
    key = ele.pop('source')
    d1[key] = d1.get(key, list())
    d1[key].append(ele)

print(d1)

output is:

{'source_name1': [{'date': ' date_value1.1', 'rate': 'rate_value1.1'}, {'date': 'date_value1.2', 'rate': 'rate_value1.2'}], 'source_name2': [{'date': 'date_value2.1', 'rate': 'rate_value2.1'}]}

2 Comments

Does not match with what OP needs.
Or you could also do, d1.setdefault(ele.pop('source'), []).append(ele)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.