Search code examples
pythondictionarynested

Update a nested dictionary without removing existing (key : value) pair


Given these two dictionaries

d1 = {'Hulu' : 12, 'Crunchyroll' : 10, 'Cornhub' : 10, 'Twitter': 20 }#Name and Date
d2 = {'Crunchyroll': 15, 'Bulu' : 30, 'Cornhub': 50, 'Hulu': 25, 'Twitter': 100}#Name and Amount

Find any matching names and create a nested dictionary with the items from d2. Use the value from d1 as the 'Parent name'(Not sure if that's the correct terminology)

My current output:

{12: {('Hulu', 25)}, 10: {('Crunchyroll', 15)}, 'Cornhub': 50, 20: {('Twitter', 100)}}

My expected output:

{12: {('Hulu', 25)}, 10: {('Crunchyroll', 15), ('Cornhub', 50)}, 20: {('Twitter', 100)}}

Here is the complete code I have so far

d1 = {'Hulu' : 12, 'Crunchyroll' : 10, 'Cornhub' : 10, 'Twitter': 20 }#Name and Date
d2 = {'Crunchyroll': 15, 'Bulu' : 30, 'Cornhub': 50, 'Hulu': 25, 'Twitter': 100}#Name and Amount
dp = {}
total = 0
space = ' '

for x in d1.items() :
    #print(x)
    for i in d2.items() :
        #print(x,":",i)
    #Filter based of names
        if x[0] == i[0] and x[1] not in dp.keys() :
            #print('Date', x[1], 'Bill:Amount', i)
            dp[x[1]] = {i}
            total += i[1]
        elif x[0] == i[0] and x[1] in dp.keys() :
            #print('elif', i)
            total += i[1]
            dp.update({i})
#print(sorted(dp.items(), key=lambda x:x))
print('Up coming bills', dp, '\n''Total= $',total)

I have also tried this with-in the elif instead of the update but that failed as well and over wrote the existing key:value items

dp[x[1]] = {i[0] : i[1]}

Solution

  • To get the expected output you can use a single loop. This will increase performance:

    d1 = {'Hulu': 12, 'Crunchyroll': 10, 'Cornhub': 10, 'Twitter': 20}
    d2 = {'Crunchyroll': 15, 'Bulu': 30, 'Cornhub': 50, 'Hulu': 25, 'Twitter': 100}
    
    result = {}
    
    for key, value in d1.items():
        if value not in result:
            result[value] = set()
        if key in d2:
            result[value].add((key, d2[key]))
    
    print(result)
    

    If you want to optimize the code further you can use defaultdict like this

    from collections import defaultdict
    
    d1 = {'Hulu': 12, 'Crunchyroll': 10, 'Cornhub': 10, 'Twitter': 20}
    d2 = {'Crunchyroll': 15, 'Bulu': 30, 'Cornhub': 50, 'Hulu': 25, 'Twitter': 100}
    
    result = defaultdict(set)
    
    for key in d1:
        if key in d2:
            result[d1[key]].add((key, d2[key]))
    
    print(dict(result))
    

    You can change the logic as per your need