Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a nested dictionary (pythonic way) for a specific value using map or filter instead of list comprehensions?

I've a nested dictionary.

>>> foo = {'m': {'a': 10}, 'n': {'a': 20}}
>>> 

I'd like to filter specific values, based on the values of 'a'.

I can use list comprehensions for the purpose.

>>> [foo[n] for n in foo if foo[n]['a'] == 10]
[{'a': 10}]
>>> 

Using list alone gives me the elements from foo (and not the values of the elements) - as expected:

>>> list(filter(lambda x: foo[x] if foo[x]['a']==10 else None,foo))
['m']
>>> 

Using map returns me unwanted 'None' values:

>>> list(map(lambda x: foo[x] if foo[x]['a']==10 else None,foo))
[{'a': 10}, None]
>>> 

Combining these two, I can fetch the desired value. But I guess foo is iterated twice - once each for filter and map. The list comprehension solution needs me to iterate just once.

>>> list(map(lambda t: foo[t], filter(lambda x: foo[x] if foo[x]['a']==10 else None,foo)))
[{'a': 10}]
>>> 

Here's another approach using just filter. This gives me the desired values but I'm not sure if iterating over values of a dictionary is a good/pythonic approach:

>>> list(filter(lambda x: x if x['a'] == 10 else None, foo.values()))
[{'a': 10}]
>>> 

I'd like to know if:

  1. What's the pythonic/recommended approach for this scenario?
  2. If the last example using filter on dictionary values is an acceptable?

Regards

Sharad

like image 331
Sharad Avatar asked Aug 27 '16 07:08

Sharad


Video Answer


2 Answers

A list comprehension can do this beautifully.

>>> foo = {'m': {'a': 10}, 'n': {'a': 20}}
>>> [v for v in foo.values() if 10 in v.values()]
[{'a': 10}]

You don't need the for loop or the list comprehension if you are matching against a known key in the dictionary.

In [15]: if 10 in foo['m'].values():
    ...:     result = [foo['m']]
    ...:     

In [16]: result
Out[16]: [{'a': 10}]
like image 98
styvane Avatar answered Oct 13 '22 16:10

styvane


If you want to return the full nested dictionary if it contains a, a list comprehension is probably the cleanest way. Your initial list comprehension would work:

[foo[n] for n in foo if foo[n]['a'] == 10]

You can also avoid the lookup on n with:

[d for d in foo.values() if d['a'] == 10]

List comprehensions are generally considered more Pythonic than map or filter related approaches for simpler problems like this, though map and filter certainly have their place if you're using a pre-defined function.

This gives me the desired values but I'm not sure if iterating over values of a dictionary is a good/pythonic approach.

If you want to return all values of a dictionary that meet a certain criteria, I'm not sure how you would be able to get around not iterating over the values of a dictionary.

For a filter-based approach, I would do it as:

list(filter(lambda x: x['a'] == 10, foo.values()))

There's no need for the if-else in your original code.

like image 40
Karin Avatar answered Oct 13 '22 17:10

Karin