'Python - create new dictionary if condition from another dictionary is met [closed]

I have two dictionaries conj_dict and score_dict. The keys of both dictionaries are the same.

I would like to create a new dictionary with all the key pairs of conj_dict that have a value in score_dict of less than 2.

conj_dict = {
  "'Essere' PR 1": 'sono',
  "'Essere' PR 2": 'sei',
  "'Essere' PR 3": 'e',
  "'Essere' PR 4": 'siamo',
  "'Essere' PR 5": 'siete',
  "'Essere' PR 6": 'sono'
    }

score_dict = {
  "'Essere' PR 1": 0,
  "'Essere' PR 2": 2,
  "'Essere' PR 3": 1,
  "'Essere' PR 4": 0,
  "'Essere' PR 5": 0,
  "'Essere' PR 6": 0
    }


Solution 1:[1]

Make an empty dict and loop over either dictionary and use the key to access the respective values of each.

d = {}
for k, v in conj_dict.items():
    if(score_dict.get(k) < 2): d[k] = conj_dict.get(k)

OR

d = {}
for k, v in score_dict.items():
    if(score_dict.get(k) < 2): d[k] = conj_dict.get(k)

Showcasing functionality of all values of score < 2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicholas Chmielewski