Home > Blockchain >  Python - Iterate over a dict with list as values and create new dict based on list values
Python - Iterate over a dict with list as values and create new dict based on list values

Time:01-29

I need to create a new dict for each value in the list for each key

dict = {'app_clicks': [56, 65, 41, 40, 64],
 'billed_charge_local_micro': [219941307,
                               247274715,
                               181271175,
                               164359644,
                               223745830],
 'billed_engagements': [85831, 89976, 60566, 55304, 88839],
 'card_engagements': None,
 'carousel_swipes': None,
 'clicks': [322, 351, 225, 197, 337],
 'engagements': [363, 397, 258, 233, 383],
 'follows': None,
 'impressions': [86236, 90763, 60596, 55689, 88916],
 'likes': [4, 2, 3, 1, 8],
 'media_engagements': [41, 45, 33, 36, 46],
 'media_views': [33533, 35665, 23611, 21957, 35792],
 'poll_card_vote': None,
 'qualified_impressions': None,
 'replies': [0, 1, 0, 0, 0],
 'retweets': None,
 'tweets_send': None,
 'unfollows': None,
 'url_clicks': [56, 65, 41, 40, 64],
 'video_15s_views': [27859, 29801, 19852, 18974, 30373],
 'video_3s100pct_views': [16441, 17699, 11112, 10337, 16993],
 'video_6s_views': [17332, 18785, 12126, 11517, 18663],
 'video_content_starts': [81824, 85312, 58449, 53392, 84893],
 'video_cta_clicks': None,
 'video_total_views': [33533, 35665, 23611, 21957, 35792],
 'video_views_100': [27861, 29774, 19840, 18982, 30386],
 'video_views_25': [72137, 75452, 51049, 46138, 74744],
 'video_views_50': [48377, 50961, 34242, 31603, 51323],
 'video_views_75': [35233, 37444, 24959, 23430, 38004]}

And basically I need to create a new list with dicts for each value on the list.

So it would be something like this

   new_list_with_dicts = [
       { "billed_charge_local_micro" : 219941307,
         "clicks": 322,
         "impressions" : 86236,
       },
       { "billed_charge_local_micro" : 247274715,
         "clicks": 351,
         "impressions" : 90763,
       },
       { "billed_charge_local_micro" : 181271175,
         "clicks": 225,
         "impressions" : 60596,
       },
    ]

This is one of the ways I tried:

    i = 0
    list_data = []
    dict = {}
    for key,value in report.items():

        if isinstance(value, list):
            while i < len(value):
                dict[key] = value[i]

                pprint.pprint(dict)
                i = i   1
            i = 0

But it is creating the dict with repeated values, apparently for the last item of each array

I hope I could explain clearly

I'm stuck in this so any help would be much appreciated.

Thanks in advance

CodePudding user response:

I belive this solves the problem, it seems like you were almost there but messed up some of the syntax

list_data = [{} for i in range(len(list(report.values())[0]))]
for key,value in report.items():
    if isinstance(value, list):
        for i in range(len(value)):
            list_data[i][key] = value[i]
print(list_data)

and this should return the desired output assuming your dictionary is stored in report

  •  Tags:  
  • Related