Merging 2 dicts into 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Merging 2 dicts into 1

I have 2 dicts d1 and d2, how do I merge them into 1 such that it contains both the key value pairs from each dictionary ? also, suppose the values are in a list, how do I append the values of d2 to d1 if they share a common key ? what I have so far is this, but the results are different and when I compare the result with my expected result it sometimes gives true and sometimes false https://code.sololearn.com/c93v2Pzd3t6b/?ref=app

11th May 2017, 6:26 PM
Rockiecoder
Rockiecoder - avatar
9 Answers
+ 3
I looked at your code again. If you replace everything in the innermost for loop with d[k1] = d1[k1] + d2[k1] you will not need to make a second copy of d1 and everything should work. Your thinking was fine, just got one technical detail wrong. Don't worry if you don't understand my code, it's using some concepts you did probably not learn yet. The Python course here will teach you the most important ideas though, so keep it up! If I could help you, you can mark your favourite answer as best. Also consider following me and Kuba Siekierzyński, you can probably pick something up from our codes, like https://code.sololearn.com/ck8ik6lkuKwj or https://code.sololearn.com/cZlLfe9tWoP2
11th May 2017, 11:20 PM
Tob
Tob - avatar
+ 3
Hint: You modify the dictionary d1 in your mergedict function and that will result in unexpected behaviour. If you delete the line print(mergedict(d1,d2)) the following check will return True. If you were to repeat that check, you'd get False.
11th May 2017, 7:30 PM
Tob
Tob - avatar
+ 3
Yes, you can easily check that if you change your code like this: https://code.sololearn.com/c76n1GH0u7YF If you pass d1 to your function, what you really pass is a reference to that object, so changes made to the d1 in your function affect the dict you plugged in as the first argument. One way around that would be to make an extra copy of d1 to modify. When I get the time, I can look into it and see if I can come up with a solution, which avoids unnecessary copying.
11th May 2017, 9:00 PM
Tob
Tob - avatar
+ 3
This would be a kind of show-offy one line solution: https://code.sololearn.com/chw2B8hAZDa0 Should run way faster and consume less memory than any solution using copies would.
11th May 2017, 10:43 PM
Tob
Tob - avatar
+ 1
Definitely! I'm also new to this app and am looking forward to learning as much as I can, thanks for the help 😀
11th May 2017, 11:32 PM
Rockiecoder
Rockiecoder - avatar
+ 1
A rather minimalistic solution would be as follows: Loop through the keys of the second dictionary. If the key already exists in the first dict, simply append the list of values (from the 2nd dict) to that of the 1st dict If the key does not exist in the 1st dict, just insert it, together with the values from the 2nd dict. def merge_dicts(first, other): merged = first.copy() for key in other.keys(): if key in merged.keys(): merged[key].append(other[key]) else: merged[key] = other[key] return merged
12th May 2017, 5:34 PM
Klaus-Dieter Warzecha
Klaus-Dieter Warzecha - avatar
0
Thanks for the reply, Tobi I see what you mean, I didn't expect the Boolean statement to actually execute the command again. But I do have a question regarding to ur first paragraph. are you implying that my code modifies d1 and therefore it is giving me unexpected answers and therefore my code is not going to work as desired ? also I have added a little more code in the beginning to make sure that if 2 dicts do not share similar keys, then the resulting dict Is just going to be performed by the first 3 lines of my code. Thanks again! https://code.sololearn.com/c93v2Pzd3t6b/?ref=app
11th May 2017, 8:12 PM
Rockiecoder
Rockiecoder - avatar
0
I think I know what you mean now... I seem to have the changed the original dict d1 that I was passed with by executing the the for loops that loops through the d2[k2] list. would it be ok if I just used another variable and assign that to be = d1.copy? I can't wrap my head around whether or not I should be copying the dicts, im starting to think that there is no need to create copies of dicts to go about this code Thanks in advance
11th May 2017, 10:25 PM
Rockiecoder
Rockiecoder - avatar
0
I forgot to state that I started coding (never coded before) 3 months ago, and therefore I cannot make sense of that piece of code you did there, but wow did it impressed me! never thought you could actually write a code that does all the work in 1 line! for now I'm just trying to make the code work and worry less about memory consumption, and just wanted to know if my piece of code can be able to do the job for now. thanks so much for the answers cheers!
11th May 2017, 10:57 PM
Rockiecoder
Rockiecoder - avatar