Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dictionaries in different lengths comparison in Python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading