1
def combine_guests(guests1, guests2):
  # Combine both dictionaries into one, with each key listed 
  # only once, and the value from guests1 taking precedence

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}

print(combine_guests(Rorys_guests, Taylors_guests))

Want this output

{"Adam": [2, 1], "Branda": 3, "David": [1, 4] ...}

with value from guests1 i.e.Rorys_guests dict will take precedence first .i.e for Adam key value is [2,1]

Sergey Shubin
  • 2,689
  • 4
  • 23
  • 29
nishikant gurav
  • 47
  • 1
  • 2
  • 7

11 Answers11

2
def combine_guests(guests1, guests2):
    #Update function will add new values and update existing values in the dictionary
    guests2.update(guests1)
    return guests2

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}

print(combine_guests(Rorys_guests, Taylors_guests))
1

Python 3.5 or above

z = {**Rorys_guests , **Taylors_guests }
Nayanish Damania
  • 479
  • 5
  • 13
  • Correct answer. Just to be clear with the given example combined_guests = {**Rorys_guests, **Taylors_guests} – Meshi Mar 05 '20 at 12:33
  • i done that but issue is combine both dictionaries into one, with each friend listed only once, and the number of guests from Rory's dictionary taking precedence, if a name is included in both dictionaries. Then print the resulting dict – nishikant gurav Mar 05 '20 at 12:36
0

This should work:

def combine_guests(guests1, guests2):
    out = guests1.copy()
    for key, val in guests2.items():
        out[key] = val
    return out

EDIT: copied guests2 instead of guests1

  • this prints only taylors_guests dict but we want to combine both in one with value from dict2 taking presedence...e.g{"Adam":[2,1],"Branda":3,"David:[1,4]....} – nishikant gurav Mar 05 '20 at 12:55
0

This might help. This will give precedence to Rorys_guests and will not include those from Taylors_guests if both have the value.

Taylors_guests.update(Rorys_guests)
return Rorys_guests
maestromusica
  • 695
  • 7
  • 23
0

Try this it will work

def combine_guests(guests1, guests2):
    lst ={}
  # Combine both dictionaries into one, with each key listed
  # only once, and the value from guests1 taking precedence
    lst =guests1
    for name,friend in guests2.items():
        if name in lst:
             pass
        else:
            lst[name] =friend
    return lst
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}

print(combine_guests(Rorys_guests, Taylors_guests))

Open for any kind of suggestions

0
def combine_guests(guests1, guests2):
  # Combine both dictionaries into one, with each key listed 
  # only once, and the value from guests1 taking precedence
    combine = guests1
    for guest, friend in guests2.items():
      if guest not in combine:
        combine[guest] = friend
    return combine

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}

print(combine_guests(Rorys_guests, Taylors_guests))
סטנלי גרונן
  • 2,849
  • 23
  • 46
  • 65
0
enter code here: def combine_guests(guests1, guests2):
                     empty={}
                     empty.update(guests1)
                     empty.update(guests2)
                     for key,val in guests1.items():
                         empty[key]=val
                     return(empty)

   
Shalini S
  • 1
  • 1
0
def combine_guests(guests1, guests2):
  precedence=''

  guests2.update(guests1)

  for key,value in guests2.items():

    if key in guests1:
      precedence=value+1
      guests2[key]=precedence
  return guests2

output:

{'David': 2, 'Nancy': 1, 'Robert': 5, 'Adam': 3, 'Samantha': 3, 'Chris': 5, 'Brenda': 4, 'Jose': 4, 'Charlotte': 3, 'Terry': 2}
David Buck
  • 3,594
  • 33
  • 29
  • 34
  • check indentations. Its currently incorrect and also add context to prevent down-voting and to make your answer quality enough to compete with the other seven answers. EoR. Welcome and enjoy SO ;-) – ZF007 Nov 07 '20 at 11:04
0
def combine_guests(guests1, guests2):
  # Combine both dictionaries into one, with each key listed 
  # only once, and the value from guests1 taking precedence
  guests1.update(guests2)
  return guests1
Rohan Kumara
  • 107
  • 2
  • 8
0
def combine_guests(guests1, guests2):
    # Combine both dictionaries into one, with each key listed
    # only once, and the value from guests1 taking precedence
    for key,value in guests2.items():
        if key in guests1:
            if guests1[key] < guests2[key]:
                guests1[key] = value
        else:
            guests1[key] = value
    return guests1

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}

print(combine_guests(Rorys_guests, Taylors_guests))
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
  • Unfortunately this is not a correct answer as the question was unclear about the desired output. Fixed the question — the desired output was in the comments. – Sergey Shubin Mar 18 '21 at 09:05
0

This is a bit late, as the OP submitted their question some time ago, but I guess I will submit my answer:

def combine_guests(guests1, guests2):
   # Combine both dictionaries into one, with each key listed 
   # only once, and the value from guests1 taking precedence
   guests2.update(guests1)
   return guests2
Pentevrien
  • 21
  • 3