I am trying to figure out how to compare two dictionaries having different number of keys. For instance, here are two dictionaries:
person = {'name': 'John', 'birthYear': 1995, 'month': 1}
time = {'birthYear': 1995, 'month': 1}
I want to write code that if the second(birthYear) and third(month) key in person matches the first(birthYear) and second(month) key in time, the program will print out the name for the person (or in comparison just call it true). Is there a way to do so? I am pretty new to Python.
>Solution :
Try something like:
person = {'name': 'John', 'birthYear': 1995, 'month': 1}
time = {'birthYear': 1995, 'month': 1}
if all(person[k] == time[k] for k in ['birthYear', 'month']):
print(person['name'])