Skip to content Skip to sidebar Skip to footer

Filtering Dict Of Dict With Dictionary Comprehension

I have problem with filtering dict of dict while using dict comprehension. I have dict: clients = { 'Shop': {'url' : 'url_v', 'customer' : 'cumoster_v', '

Solution 1:

>>> {key:{k:v for k,v in dic.items() if 'customer' in k} for key,dic in clients.items()}
{'Shop': {'customer': 'cumoster_v'}, 'Gym': {'customer_2': 'customer_v2', 'customer_1': 'customer_v1'}, 'Bank': {'customer_3': 'customer_v3'}}

Solution 2:

{k:{k1:v1 for  k1,v1 in v.items() if k1.startswith('customer')} for k, v in clients.items()}

Output :

{'Shop': {'customer': 'cumoster_v'}, 'Gym': {'customer_2': 'customer_v2', 'customer_1': 'customer_v1'}, 'Bank': {'customer_3': 'customer_v3'}}

Post a Comment for "Filtering Dict Of Dict With Dictionary Comprehension"