Python Forum

Full Version: Union of dictionaries (taking max value on collision)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i need to write a program that takes two dictionaries and returns their union my problem is that if key is found in both, the larger value should be
used in the output.
example for dictionaries:
{'A':1,'B':5,'C':3},{'B':4, 'C':5, 'D':6})
and what do you have so far?
(Jan-17-2018, 08:30 PM)metulburr Wrote: [ -> ]New Reply 
Top Page
Python Forum › Python Coding › General Coding Help
need help


im new in python so im not sure about it
dicA=[A:a,B:b,C:c,D:d]
dicB=[A:w,B:x,C:y,D:z]

dict_union=dicA.update(dicB)
return(dict_union)
There is a syntax error at line 1. My python interpreter says
  File "updict.py", line 1
    dicA=[A:a,B:b,C:c,D:d]
           ^
SyntaxError: invalid syntax
changed it to
dicA=['A':a,'B':b,'C':c,'D':d]
dicB=['A':w,'B':x,'C':y,'D':z]

dict_union=dicA.update(dicB)
print(dict_union)
You need to actually try to run the code you are providing or this process isn't going to work.
  File "arghlebargle.py", line 1
    dicA=['A':a,'B':b,'C':c,'D':d]
             ^
SyntaxError: invalid syntax
Dictionaries are declared with {} not []