1

I have three dictionaries that I would like to combine:

dict_A = {'Archives': '1234',
          'Content Revenue': '4567'}

dict_B = {'Archives': {'Content Partners Draft': '1111',
                       'Revenue Dashboard': '2222',
                       'Customer Lifetime Value Dashboard': '333',
                       'Customer Liftime Value V3': '4444'},
          'Content Revenue': {'License Tracker': '5555',
                              'MONTH_END_PARTNER_REPORT': '6666',
                              'Par': '7777',
                              'Report': '8888',
                              'Impressions': '9999'}}

dict_C = {'Content Partners Draft': {'Content Partner Payout': '1111',
                                     'Device Partner Payments': '1111',
                                     'FirstRights (FirstRights xReference)': '1111',
                                     'Sheet1 (Combined Partner Rev Share & Usage Rev Share)': '1111',
                                     'Sheet1 (Rev Share xReference)': '1111'},
          'Revenue Dashboard': {'Device Breakdown CPM and Fill Rate': '1111',
                                'Device CPM and Fill Rate Targets': '1111',
                                'Platform Breakdown CPM and Fill Rate': '1111',
                                'Platform CPM and Fill Rate Targets': '1111'},
          'Customer Lifetime Value Dashboard': {'LTV Data Source 1-9-2020 Extract': '1111'},
          'Customer Liftime Value V3': {'Customer LTV Data Source': '1111'},
          'License Tracker': {'Sheet1 (Licensing)': '1111'},
          'MONTH_END_PARTNER_REPORT': {'impressions_monthly': '1111', 'kpis_monthly_est': '1111'},
          'Par': {'impressions_monthly': '1111', 'kpis_daily_est': '1111', 'kpis_monthly_est': '1111', 'timelines': '1111'},
          'Report': {'kpis_monthly_est': '1111', 'partner_kpis_prog_hourly (public)': '1111'},
          'Impressions': {'impressions_daily': '1111', 'impressions_monthly': '1111'}}

I try to combine them with this nested dict comprehension:

combined_dict = {
    outer_key : {nested_key : nested_value 
                 for nested_key in dict_B[outer_key]
                 for nested_value in dict_C[nested_key]}
    for outer_key in dict_A}

and I get this result:

{'Archives': {'Content Partners Draft': 'Sheet1 (Rev Share xReference)',
              'Customer Lifetime Value Dashboard': 'LTV Data Source 1-9-2020 '
                                                   'Extract',
              'Customer Liftime Value V3': 'Customer LTV Data Source',
              'Revenue Dashboard': 'Platform CPM and Fill Rate Targets'},
 'Content Revenue': {'Impressions': 'impressions_monthly',
                     'License Tracker': 'Sheet1 (Licensing)',
                     'MONTH_END_PARTNER_REPORT': 'kpis_monthly_est',
                     'Par': 'timelines',
                     'Report': 'partner_kpis_prog_hourly (public)'}}

The result combines the values from dict_A and dict_B successfully, but only includes one value from dict_C. Is it possible to have a list of values (keys) as a value in a nested dictionary comprehension?

Thanks!

My expected output would include a full list of keys as the nested values:

{'Archives': {'Content Partners Draft': ['Content Partner Payout', 'Device Partner Payments', 'FirstRights (FirstRights xReference)', 
                                         'Sheet1 (Combined Partner Rev Share & Usage Rev Share)', 'Sheet1 (Rev Share xReference)'],
              'Customer Lifetime Value Dashboard': ['LTV Data Source 1-9-2020 Extract'],
              'Customer Liftime Value V3': ['Customer LTV Data Source'],
              'Revenue Dashboard': ['Device Breakdown CPM and Fill Rate', 'Device CPM and Fill Rate Targets', 'Platform Breakdown CPM and Fill Rate', 
                                    'Platform CPM and Fill Rate Targets']}
{'Content Revenue': {'Impressions': ['impressions_daily', 'impressions_monthly'],
                     'License Tracker': ['Sheet1 (Licensing)'],
                     'MONTH_END_PARTNER_REPORT': ['impressions_monthly', 'kpis_monthly_est'],
                     'Par': ['impressions_monthly', 'kpis_daily_est', 'kpis_monthly_est', 'timelines'],
                     'Report': ['kpis_monthly_est', 'partner_kpis_prog_hourly (public)']}}
2
  • 3
    What is your expected output? Commented Mar 10, 2020 at 21:14
  • As mentioned please specified your expected output, it is unclear what you really expect your code to do. Also a possible issue could arise from the use of double for in a dict comprehension, this would result in duplicate keys and therefore value overwriting. Commented Mar 10, 2020 at 21:20

1 Answer 1

1

you could use:

combined_dict = {
    outer_key: {nested_key: list(dict_C[nested_key].keys())
                for nested_key in dict_B[outer_key]}
    for outer_key in dict_A}

output:

{'Archives': {'Content Partners Draft': ['Content Partner Payout',
   'Device Partner Payments',
   'FirstRights (FirstRights xReference)',
   'Sheet1 (Combined Partner Rev Share & Usage Rev Share)',
   'Sheet1 (Rev Share xReference)'],
  'Revenue Dashboard': ['Device Breakdown CPM and Fill Rate',
   'Device CPM and Fill Rate Targets',
   'Platform Breakdown CPM and Fill Rate',
   'Platform CPM and Fill Rate Targets'],
  'Customer Lifetime Value Dashboard': ['LTV Data Source 1-9-2020 Extract'],
  'Customer Liftime Value V3': ['Customer LTV Data Source']},
 'Content Revenue': {'License Tracker': ['Sheet1 (Licensing)'],
  'MONTH_END_PARTNER_REPORT': ['impressions_monthly', 'kpis_monthly_est'],
  'Par': ['impressions_monthly',
   'kpis_daily_est',
   'kpis_monthly_est',
   'timelines'],
  'Report': ['kpis_monthly_est', 'partner_kpis_prog_hourly (public)'],
  'Impressions': ['impressions_daily', 'impressions_monthly']}}

this line for nested_value in dict_C[nested_key]} cause you to have only one key from dict_C in the final result, it sets for the same key nested_key all the values from dict_C[nested_key], in a dict one key can have only one value

Sign up to request clarification or add additional context in comments.

Comments

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.