Skip to content
Sign upLog in

I want a help with dictionary problem

Profile icon
azza1551

1 - How to write a function that takes a dictionary and a key and determines if that key is in the dictionary (i.e. the function outputs True if the key is in the dictionary, and False otherwise). This should output: True and False

2 - How to write a function that takes two dictionaries with unique keys and merge them.
This should output: {'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

You are viewing a single comment. View All
Profile icon
Coder100
  1. to check if a key is in a dictionary, use the in keyword
def key_is_in(d, k): return k in d
  1. to join 2 dictionaries together, use a for loop!
def join_dicts(a, b): out = {} for keya in a: out[keya] = a[keya] for keyb in b: out[keyb] = b[keyb] return out