- #1
ver_mathstats
- 260
- 21
- Homework Statement
- Write a Python code to concatenate two dictionaries; if the two dictionaries have the same key, then the values corresponding to this key will be
appended.
- Relevant Equations
- Python
Code:
dict1={"a":1,"b":2}
dict2={"a":3, "d":4}
for k in dict2:
if k in dict1:
dict2[k] = dict1.append(dict2)
dict1.update(dict2)
print(dict1)
I need the result to be {'a': [1, 3], 'b': 2, 'd': 4} and I am confused as to how to achieve this as I always get an error message. I've tried this as well dict2[k] = dict2[k].append(dict1[k]). Any help is appreciated, thank you.