Stagg Stagg - 7 months ago 13
Python Question

Merge to different dictionaries

I have two lists of dictionaries like this:

dic1 = [{'Age': 8, 'Pets Name': 'Felix', 'Species': 'Cat'}, {'Age': 57, 'Pets Name': 'Michelangelo', 'Species': 'Tortoise'}, {'Age': 12, 'Pets Name': 'Rantanplan', 'Species': 'Dog'}, {'Age': '2', 'Pets Name': 'Nemo', 'Species': 'Fish'}, {'Age': '45', 'Pets Name': 'Leonardo', 'Species': 'Tortoise'}, {'Age': 9, 'Pets Name': 'Milo', 'Species': 'Dog'}, {'Age': 57, 'Pets Name': 'Raphael', 'Species': 'Tortoise'}, {'Age': '4', 'Pets Name': 'Dory', 'Species': 'Fish'}]
dic2 = [{'Pets Names': 'Michelangelo', "Owner's Names": 'Ana'}, {'Pets Names': 'Dory', "Owner's Names": 'Eva'}, {'Pets Names': 'Rantanplan', "Owner's Names": 'Ada'}, {'Pets Names': 'Leonardo', "Owner's Names": 'Ana'}, {'Pets Names': 'Felix', "Owner's Names": 'Eva'}, {'Pets Names': 'Raphael', "Owner's Names": 'Ana'}, {'Pets Names': 'Nemo', "Owner's Names": 'Eva'}]


I need to make a third list of dictionaries, being the keys (of the dictionaries) the owner's names, and the values the age of their respective pets. For example, given the two lists above, the output should be:

dic3 = [{'Ana': [57, 45, 57]}, {'Eva': [4, 8, 2]}, {'Ada': [12]}]


The main goal is to get the average of the each owner's pets ages but I think I can do it myself after I have the desired dict.
So far I only have this:

def merge_dicts(dic1,dic2):

name = [li['Pets Name'] for li in dic1]
age = [li['Age'] for li in dic1]
owenrsName = [li["Owner's Names"] for li in dic2]


This gives me the lists of the name of the pets, their age, and the name of the owner's, but I don't know if it's relevant for the final function.

Thanks in advance.

Answer
dic1 = [{'Age': 8, 'Pets Name': 'Felix', 'Species': 'Cat'}, {'Age': 57, 'Pets Name': 'Michelangelo', 'Species': 'Tortoise'}, {'Age': 12, 'Pets Name': 'Rantanplan', 'Species': 'Dog'}, {'Age': '2', 'Pets Name': 'Nemo', 'Species': 'Fish'}, {'Age': '45', 'Pets Name': 'Leonardo', 'Species': 'Tortoise'}, {'Age': 9, 'Pets Name': 'Milo', 'Species': 'Dog'}, {'Age': 57, 'Pets Name': 'Raphael', 'Species': 'Tortoise'}, {'Age': '4', 'Pets Name': 'Dory', 'Species': 'Fish'}]
dic2 = [{'Pets Names': 'Michelangelo', "Owner's Names": 'Ana'}, {'Pets Names': 'Dory', "Owner's Names": 'Eva'}, {'Pets Names': 'Rantanplan', "Owner's Names": 'Ada'}, {'Pets Names': 'Leonardo', "Owner's Names": 'Ana'}, {'Pets Names': 'Felix', "Owner's Names": 'Eva'}, {'Pets Names': 'Raphael', "Owner's Names": 'Ana'}, {'Pets Names': 'Nemo', "Owner's Names": 'Eva'}]



def merge_dicts(dic1,dic2):
    dict3= {}
    result = []
    name = [li['Pets Name'] for li in dic1]
    age = [li['Age'] for li in dic1]
    owenrsName = [li["Owner's Names"] for li in dic2]


    for d2 in dic2:

       pname = d2['Pets Names']
       oname = d2["Owner's Names"]

       if oname not in dict3:
            dict3[oname] = []

       for d1 in dic1:
           if d1['Pets Name'] == pname:
               dict3[oname].append(d1['Age'])
    for key,value in dict3.items():
        result.append({key:value})

    print(result)


merge_dicts(dic1,dic2)

o/p

[{'Ada': [12]}, {'Eva': ['4', 8, '2']}, {'Ana': [57, '45', 57]}]
Comments