0

I have 2 Lists of dictionaries, example below:

list one = [
  {'day': '2021-09-05 00:00:00+02:00', 'airtime_total_for_day': '650.00', 'data_total_for_day': '0'},
  {'day': '2021-09-16 00:00:00+02:00', 'airtime_total_for_day': '124.20', 'data_total_for_day': '20.00'},
]

list_two = [
  {'day': '2021-09-10 00:00:00+02:00', 'data_bundle_total_for_day': '100.00'},
  {'day': '2021-09-16 00:00:00+02:00', 'data_bundle_total_for_day': '50.00'},
  {'day': '2021-09-18 00:00:00+02:00', 'data_bundle_total_for_day': '45.00'},
]

I would like to merge the two lists together and at the same time, merge data_total_for_day with data_bundle_total_for_day under the name data_total_for_day.

Also, if data_total_for_day & data_bundle_total_for_day happen to fall on the same day which they do on the 2021-09-16, the values need to be added together e.g.

  • data_total_for_day on the 2021-09-16 is: 20.00
  • data_bundle_total_for_day on the 2021-09-16 is: 50.00

So on that specific day, data_total_for_day needs to be: 70.00

I've struggled and all & any help with be greatly appreciated.

Things I tried:

KD1
  • 107
  • 1
  • 15
  • 1
    How are these lists being generated? Are they the result of calling `values()` on two querysets? – Iain Shelvington Sep 21 '21 at 12:53
  • @IainShelvington this is generated in django. I created a query using the ORM. – KD1 Sep 21 '21 at 12:59
  • It's probably possible to do this by combining your queries into one. Can you add your model(s) and queries to the question? – Iain Shelvington Sep 21 '21 at 13:05
  • hi @IainShelvington unfortunately I can't do that :(, it's for a client & he/she says I can't do that. Privacy issue. – KD1 Sep 21 '21 at 13:10
  • Understandable, even an obfuscated example? Your totals appear to be strings which could cause issues, unless parsing them needs to be part of the solution? – Iain Shelvington Sep 21 '21 at 13:28

0 Answers0