2

I have these 2 dictionaries:

Rorys_guests = {"Adam": 2, "Brenda": 3, "David": 1, "Jose": 3, "Charlotte": 2, "Terry": 1, "Robert": 4}

Taylors_guests = {"David": 4, "Nancy": 1, "Robert": 2, "Adam": 1, "Samantha": 3, "Chris": 5}

I want to check if a key (only the key) in Taylors_guests is already in Rorys_guests. If not, I want to append the key and value pair from Taylors to Rorys.

Note: Some key are the same in both dictionaries. I don't want to overwrite the values in Rorys_guests. I only want to append keys and values from Taylors dict that are not yet in the Rorys dict.

for i in Taylors_guests:
    print(i)
    if i in Rorys_guests:
        print("yes")
    else:
        Rorys_guests = Rorys_guests.get(i)

print(Rorys_guests)

I am a python noob, but still looked through many different sites and couldn't find a solution.

Thank you in advance!

hr2406
  • 23
  • 4
  • Does this answer your question? [How do I merge two dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression) – Mark Snyder Feb 28 '20 at 23:28
  • Sorry, @MarkSnyder, that indeed helps! – hr2406 Feb 28 '20 at 23:36

4 Answers4

2
Rorys_guests = {**Rorys_guests, **Taylors_guests}
# {'Adam': 2, 'Brenda': 3, 'David': 1, 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': 4, 'Nancy': 1, 'Samantha': 3, 'Chris': 5}

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 2
    Thank you! Fascinating to see how many different approaches can solve the same problem. I wish I could mark all of them as solved :) – hr2406 Feb 28 '20 at 23:48
1

I believe you are very close, but I think this should do it:

Rorys_guests = {"Adam": 2, "Brenda": 3, "David": 1, "Jose": 3, "Charlotte": 2, 
"Terry": 1, "Robert": 4}

Taylors_guests = {"David": 4, "Nancy": 1, "Robert": 2, "Adam": 1, "Samantha": 
3, "Chris": 5}

for k,v in Taylors_guests.items():
   if k not in Rorys_guests.keys():
      Rorys_guests[k] = Taylors_guests[k]
print(Rorys_guests)

Output:

{'Adam': 2, 'Brenda': 3, 'David': 1, 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': 4, 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
de_classified
  • 1,927
  • 1
  • 15
  • 19
1

You can try this:

Rorys_guests = {"Adam": 2, "Brenda": 3, "David": 1, "Jose": 3, "Charlotte": 2, "Terry": 1, "Robert": 4}
Taylors_guests = {"David": 4, "Nancy": 1, "Robert": 2, "Adam": 1, "Samantha": 3, "Chris": 5}

for key, value in Taylors_guests.items():
    if key not in Rorys_guests:
        Rorys_guests[key] = value
print(Rorys_guests)
de_classified
  • 1,927
  • 1
  • 15
  • 19
Errol
  • 600
  • 4
  • 16
1

You can do it in one line like this:

Rorys_guests = {"Adam": 2, "Brenda": 3, "David": 1, "Jose": 3, "Charlotte": 2, "Terry": 1, "Robert": 4}
Taylors_guests = {"David": 4, "Nancy": 1, "Robert": 2, "Adam": 1, "Samantha": 3, "Chris": 5}

Rorys_guests.update({k:v for k,v in Taylors_guests.items() if k not in Rorys_guests.keys()})
print(Rorys_guests)

>>> {'Adam': 2, 'Brenda': 3, 'David': 1, 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': 4, 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
marcos
  • 4,473
  • 1
  • 10
  • 24
  • Thank you very much! That is also a nice way. I assume similar to list comprehensions? Looks kind of familiar. Thank you! – hr2406 Feb 28 '20 at 23:42
  • @hr2406 Glad I can help, yes, it's dict comprehension (the same basically). – marcos Feb 28 '20 at 23:43