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
  • 3,040
  • 4
  • 24
  • 36
nishikant gurav
  • 47
  • 1
  • 2
  • 7

14 Answers14

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))
2

output: {'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}


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

output: {'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
Mayank
  • 21
  • 2
1

Python 3.5 or above

z = {**Rorys_guests , **Taylors_guests }
Nayanish Damania
  • 542
  • 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
  • 706
  • 8
  • 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,917
  • 23
  • 46
  • 68
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,752
  • 35
  • 31
  • 35
  • 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
zpvk
  • 124
  • 1
  • 10
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
  • 18,379
  • 16
  • 47
  • 61
  • 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
0

Kindly note the update() method i.e dict1.update(dict2) will not work here as it will defeat the purpose of the output desired by replacing the contents.

Solution:

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

Output:

{'Adam': [2, 1], 'Brenda': 3, 'David': [1, 4], 'Jose': 3, 'Charlotte': 2, 'Terry': 1, 'Robert': [4, 2], 'Nancy': 1, 'Samantha': 3, 'Chris': 5}
michaelprime_
  • 31
  • 1
  • 5
0

def combine_guests(guests1, guests2):

for key in guests2.keys():

if key in guests1:
    del guests2[key]
guests1.update(guests2)
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))

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 05 '23 at 04:52