Django Combine Common Values from Joining Two List of Dictionaries

  dictionary, django, list, merge, python

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:

Source: Python Questions

LEAVE A COMMENT