Question: I have a dictionary like below.

```
d = {'key1': {'match': '45.56', 'key12': '45.56'},
 'key2': {'key21': '45.56', 'match2': '45.56'},
 'key3': {'key31': '45.56', 'key32': '45.56'},
  'key4': ["key4", "key5"]}

```

I want to grab key names (where the value of the key is either match or match2) under heading "match" and unmatch ones under "not match" heading.

```
match
key1
key2

not match
key3
key4

```

I tried the below code but it doesn't return anything:

```
    d = {'key1': {'match': '45.56', 'key12': '45.56'},
     'key2': {'key21': '45.56', 'match2': '45.56'},
     'key3': {'key31': '45.56', 'key32': '45.56'},
      'key4': ["key4", "key5"]}

print(*[k for k in d if 'match' in d[k] or 'match2' in d[k]], sep='\n')    ---only prints the matched values

```

Answer: 
You can do a list comprehension to get the match using `any` and membership check and non-match using set difference:

```
d = {'key1': {'match': '45.56', 'key12': '45.56'},
     'key2': {'key21': '45.56', 'match2': '45.56'},
     'key3': {'key31': '45.56', 'key32': '45.56'},
      'key4': ["key4", "key5"]}

to_look = {'match', 'match2'}

match = [k for k, v in d.items() if any(x in to_look for x in v)]    
not_match = set(d).difference(match)

print('match')
print(*match, sep='\n')
print('\nnot match')
print(*not_match, sep='\n')

```

**Output**:

```
match
key1  
key2        

not match       
key3                   
key4 

```
You can iterate through the dict items and test if `match` or `match2` is in any of the keys of the sub-dicts:

```
print(*(k for k, s in d.items() if any(w in s for w in ('match', 'match2'))), sep='\n')

```

This outputs:

```
key1
key2

```

Similarly, you can get the non-matching keys by testing if both `match1` and `match2` are not in any of the keys of the sub-dicts:

```
print(*(k for k, s in d.items() if all(w not in s for w in ('match', 'match2'))), sep='\n')

```

This outputs:

```
key3
key4

```