Filtering nested dictionaries

Viewed 3291

So I have this dictionary:

Filme = {'1': {'Titel': 20, 'Jahr': 2, 'Wertung': 6, 'Preis': 50, 'Schauspielern': ['a', 'b', 'c']},
         '2': {'Titel': 30, 'Jahr': 3, 'Wertung': 7, 'Preis': 70, 'Schauspielern': ['']},
         '3': {'Titel': 10, 'Jahr': 2, 'Wertung': 6, 'Preis': 10, 'Schauspielern': ['']},
         '4': {'Titel': 5, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
         '5': {'Titel': 8, 'Jahr': 3, 'Wertung': 10, 'Preis': 100, 'Schauspielern': ['']},
         '6': {'Titel': 20, 'Jahr': 2, 'Wertung': 6, 'Preis': 10, 'Schauspielern': ['']},
         '7': {'Titel': 16, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
         '8': {'Titel': 20, 'Jahr': 2, 'Wertung': 6, 'Preis': 10, 'Schauspielern': ['']},
         '9': {'Titel': 90, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
         '10': {'Titel': 10, 'Jahr': 2, 'Wertung': 6, 'Preis': 10, 'Schauspielern': ['']}
        }

and I have to filter it based on the Wertung (rating), only showing movies which have a rating >= one I choose. Thing is, I'm quite a beginner in Python Programming and I have no idea how should I do this. So if Wertung == 7, I want it to print this

Filme = {'2': {'Titel': 30, 'Jahr': 3, 'Wertung': 7, 'Preis': 70, 'Schauspielern': ['']},
         '4': {'Titel': 5, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
         '5': {'Titel': 8, 'Jahr': 3, 'Wertung': 10, 'Preis': 100, 'Schauspielern': ['']},
         '7': {'Titel': 16, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
         '9': {'Titel': 90, 'Jahr': 3, 'Wertung': 9, 'Preis': 20, 'Schauspielern': ['']},
        }
2 Answers

You can just use a dictionary comprehension over the Filme.items():

In [2]: {k:v for k,v in Filme.items() if v['Wertung'] >= 7}
Out[2]:
{'2': {'Jahr': 3,
  'Preis': 70,
  'Schauspielern': [''],
  'Titel': 30,
  'Wertung': 7},
 '4': {'Jahr': 3,
  'Preis': 20,
  'Schauspielern': [''],
  'Titel': 5,
  'Wertung': 9},
 '5': {'Jahr': 3,
  'Preis': 100,
  'Schauspielern': [''],
  'Titel': 8,
  'Wertung': 10},
 '7': {'Jahr': 3,
  'Preis': 20,
  'Schauspielern': [''],
  'Titel': 16,
  'Wertung': 9},
 '9': {'Jahr': 3,
  'Preis': 20,
  'Schauspielern': [''],
  'Titel': 90,
  'Wertung': 9}}

This is equivalent to the following:

new_dict = {}
for k,v in Filme.items():
    if v['Wertung'] >= 7:
        new_dict[k] = v

Dictionary comprehension will be most straight-forward.

>>> {i:j for i,j in Filme.items() if j['Wertung'] >=7}
{'7': {'Titel': 16, 'Wertung': 9, 'Preis': 20, 'Jahr': 3, 'Schauspielern': ['']}, '5': {'Titel': 8, 'Wertung': 10, 'Preis': 100, 'Jahr': 3, 'Schauspielern': ['']}, '4': {'Titel': 5, 'Wertung': 9, 'Preis': 20, 'Jahr': 3, 'Schauspielern': ['']}, '2': {'Titel': 30, 'Wertung': 7, 'Preis': 70, 'Jahr': 3, 'Schauspielern': ['']}, '9': {'Titel': 90, 'Wertung': 9, 'Preis': 20, 'Jahr': 3, 'Schauspielern': ['']}}

If there is a chance that Wertung might be absent for any entry, you can use get() to prevent the code from failing unexpectedly.

>>> {i:j for i,j in Filme.items() if j.get('Wertung',0) >=7}
Related