I want to create a program which accepts two dictionaries and then outputs two new ones, where the first one is going to contain elements that weren't in d1
but were in d2
and the other one is going to contain d1
and then appended elements from d2
that are not in d1
.
Input:
dc1 = {'a': 3, 'b': 4, 'c': 5, 'd': 6, 'e': 7, 'f': 8, 'g': 9}
dc2 = {'e': 20, 'f': 21, 'g': 22, 'h': 23, 'i': 24, 'j': 25, 'k': 26, 'l': 27}
Output:
dc3 = {'h': 23, 'i': 24, 'j': 25, 'k': 26, 'l': 27}
dc4 = {'a': 3, 'b': 4, 'c': 5, 'd': 6, 'e': 7, 'f': 8, 'g': 9, 'h': 23, 'i': 24, 'j': 25, 'k': 26, 'l': 27}
This is the code I've tried running however I can't understand how to properly compare dictionaries using a comprehension and create dc3
and dc4
in just one line. So I'm already facing a syntax error on creating dc3
, so I haven't even started working on dc4
:
d3 = {v: k for k,v in dic2 for z,x in dic1 if v: k in x:z}
d3
Output:
d3 = {v: k for k,v in dic2 for z,x in dic1 if v: k in x:z}
^
SyntaxError: invalid syntax
If anyone can help me out how to properly use a comprehension here, I would really appreciate it.