Python Forum
Union of dictionaries (taking max value on collision) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Union of dictionaries (taking max value on collision) (/thread-7611.html)



Union of dictionaries (taking max value on collision) - meee - Jan-17-2018

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})


RE: need help - metulburr - Jan-17-2018

and what do you have so far?


RE: need help - meee - Jan-17-2018

(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)


RE: need help - Gribouillis - Jan-17-2018

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



RE: need help - meee - Jan-17-2018

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)


RE: need help - Mekire - Jan-17-2018

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 []