Home > Back-end >  Create list of dictionaries when values of original dictionary are lists
Create list of dictionaries when values of original dictionary are lists

Time:12-08

I have a dictionary where the values for each key are a list. I would like to extract a key value pair for each key in the dictionary with its corresponding element value from the list, and then store individual dictionaries in a new list.

So, if I have a simple dictionary like the following:

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 
                       12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

My desired output list would be a list of dictionaries, where each dictionary has the keys from my_dict with the corresponding value from the original values lists in my_dict:

final_list = [{'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame79.jpg', 'Labeler': 'Labeler 2'},
            {'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame1.jpg', 'Labeler': 'Labeler 2'},
            {'Folder':'2021-03-12_020000', 'Filename': '2021-03-12_020000-frame39.jpg', 'Labeler': 'Labeler 1'}]

CodePudding user response:

You can use zip for the task:

my_dict = {
    "Folder": ["2021-03-12_020000", "2021-03-12_020000", "2021-03-12_020000"],
    "Filename": [
        "2021-03-12_020000-frame79.jpg",
        "2021-03-12_020000-frame1.jpg",
        "2021-03-12_020000-frame39.jpg",
    ],
    "Labeler": ["Labeler 2", "Labeler 2", "Labeler 1"],
}

final_list = [
    {"Folder": a, "Filename": b, "Labeler": c}
    for a, b, c in zip(
        my_dict["Folder"], my_dict["Filename"], my_dict["Labeler"]
    )
]

print(final_list)

Prints:

[
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame79.jpg",
        "Labeler": "Labeler 2",
    },
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame1.jpg",
        "Labeler": "Labeler 2",
    },
    {
        "Folder": "2021-03-12_020000",
        "Filename": "2021-03-12_020000-frame39.jpg",
        "Labeler": "Labeler 1",
    },
]

CodePudding user response:

for dynamic key and value:

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

new_dict = {}
for key in my_dict:
    for idx, value in enumerate(my_dict[key]):
        if idx not in new_dict:
            new_dict[idx] = [(key,value)]
        else:
            new_dict[idx].append((key,value))
new_list = []
for key,value in new_dict.items():
    new_list.append(dict(value))

print(new_list)

output

[{'Folder': '2021-03-12_020000', 'Filename': '2021-03-12_020000-frame79.jpg', 'Labeler': 'Labeler 2'}, {'Folder': '2021-03-12_020000', 'Filename': '2021-03-12_020000-frame1.jpg', 'Labeler': 'Labeler 2'}, {'Folder': '2021-03-12_020000', 'Filename': '2021-03- 12_020000-frame39.jpg', 'Labeler': 'Labeler 1'}]

CodePudding user response:

Here is code assuming all items are inter related.

my_dict = {'Folder': ['2021-03-12_020000', '2021-03-12_020000', '2021-03-12_020000'],
           'Filename': ['2021-03-12_020000-frame79.jpg', '2021-03-12_020000-frame1.jpg', '2021-03- 12_020000-frame39.jpg'],  
           'Labeler': ['Labeler 2', 'Labeler 2', 'Labeler 1']}

result_dict = []

for i, (folder, filename, labler) in enumerate(zip(my_dict['Folder'], my_dict['Filename'], my_dict['Labeler'])):
    result_dict.append({"Folder":folder,'Filename':filename,"Labeler":labler })

print(result_dict)

output:

[
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03-12_020000-frame79.jpg",
    "Labeler": "Labeler 2"
  },
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03-12_020000-frame1.jpg",
    "Labeler": "Labeler 2"
  },
  {
    "Folder": "2021-03-12_020000",
    "Filename": "2021-03- 12_020000-frame39.jpg",
    "Labeler": "Labeler 1"
  }
]
  • Related