Q. Write a function called addDict(dict 1,dict 2 ) which computes the union of to dictionaries. it should return a new dictionary, with all the items in both its argument. if the same key appear in both arguments, feel free to pick a value from either.


You can understand by Watching video :-



Answer :-

def adddict(dict1,dict2) :
    dic = {}
    for i in dict1 :
        dic [ i ] = dict1 [i]
    for j in dict2 :
        dic [ j ] = dict2 [ j ]
    return dic

dict1=eval(input("Enter First Dictionary :- "))
dict2=eval(input("Enter Second Dictionary :- "))

print(adddict(dict1,dict2))

Output :-

Enter First Dictionary :- {"Path":5,"Walla":3,"Portal":4,"Express":7}
Enter Second Dictionary :- {"Python":9,"C++":2,"Java":7,"CSS":3}
{'Path': 5, 'Walla': 3, 'Portal': 4, 'Express': 7, 'Python': 9, 'C++': 2, 'Java': 7, 'CSS': 3}

>>> 

Enter First Dictionary :- {"Path":5,"Walla":3,"Portal":4,"Express":7,"Python":9,"C++":2}
Enter Second Dictionary :- {"Python":9,"C++":2,"Java":7,"CSS":3}
{'Path': 5, 'Walla': 3, 'Portal': 4, 'Express': 7, 'Python': 9, 'C++': 2, 'Java': 7, 'CSS': 3}

>>> 

Enter First Dictionary :- {9:"Python",2:"C++",7:"Java",3:"CSS"}
Enter Second Dictionary :- {5:"Path",3:"Walla",4:"Portal",7:"Express"}
{9: 'Python', 2: 'C++', 7: 'Express', 3: 'Walla', 5: 'Path', 4: 'Portal'}

>>> 



Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post