0

I am still learning python, so I tried to convert a list of tuples to a list of dictionaries using a function:

 query_result= [(3, 'base-certificate', 8, 2022), (2, 'case-certificate', 8, 2022), (3, 'standard-certificate', 8, 2022)]

I wrote a function that looks like this:

 def convert_query_data_for_frontend(object, time):
            if time == "month":
                data = [
                    {
                        row[1]: row[0],
                        "date": [row[2], row[3]],
                    }
                    for row in object
                ]
                return data

The result of running this function was: convert_query_data_for_frontend(query_result, 'month')

[{'base-certificate': 3, 'date': [8, 2022]}, {'case-certificate': 2, 'date': [8, 2022]}, {'standard-certificate': 3, 'date': [8, 2022]}]

It is not yet the desired result should have only one date for different dates in this case the month could be september etc, instead it should return:

[{'base-certificate': 3, 'case-certificate': 2, 'standard-certificate': 3, 'date': [8, 2022]}]

I thought just looping over the object and defining the dict would give me the desired result but didnt please what can I add or do in this case?

dave
  • 158
  • 1
  • 12
  • 1
    In your example, they all have the same date, so what do you mean "group by date"? Also - where did "standard certificate" go in your example expected output? It isn't clear what you're actually trying to do... – ShlomiF Aug 24 '22 at 20:40
  • @ShlomiF I have edited again it, the desired result it was an error and to be clear i want to have data collected for different month in this case the '8' is August then with the year '2022'. If you notice in what I am trying to do, I want to be able to have the result of one month in a dictionary and so on – dave Aug 24 '22 at 20:46

1 Answers1

1

You have two things you're trying to do here:

  1. convert from tuples to dictionaries
  2. merge dictionaries

So far you have been successful in the first one but you're missing the second.

Starting with the output of your first function we can merge items with a common key using itertools.groupby and then utilize functools.reduce to combine the dictionaries we have grouped by date. Additionally, since your data only has 1 date included I added some more dates to show off the grouping aspect

from functools import reduce
from itertools import groupby


cert_dates = [
    {'base-certificate': 3, 'date': [8, 2022]}, {'case-certificate': 2, 'date': [8, 2022]}, {'standard-certificate': 3, 'date': [8, 2022]},
    {'base-certificate': 3, 'date': [9, 2022]}, {'case-certificate': 2, 'date': [9, 2022]}, {'standard-certificate': 3, 'date': [10, 2022]},
]
grouped_list = groupby(cert_dates, key=lambda x: x['date'])
output_records = []
for _, records in grouped_list:
    output_records.append(reduce(lambda x, y: {**x, **y}, records))
    
for x in output_records:
    print(x)
# {'base-certificate': 3, 'date': [8, 2022], 'case-certificate': 2, 'standard-certificate': 3}
# {'base-certificate': 3, 'date': [9, 2022], 'case-certificate': 2}
# {'standard-certificate': 3, 'date': [10, 2022]}

clesiemo3
  • 1,099
  • 6
  • 17
  • Thank you for your anwser@clesiemo3, it took me time to understand this approach. But if you can explain the {**x, **y} part I would appreciate – dave Aug 26 '22 at 07:08
  • https://stackoverflow.com/a/26853961/4027401 This answer explains it far better than I could along with this PEP: https://peps.python.org/pep-0448/ - in short though it creates a dictionary from the unpacked key:value pairs in each of those dictionaries – clesiemo3 Aug 31 '22 at 17:10