How to use 'python filter dict' in Python

Every line of 'python filter dict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
30def pyfilter(func, seq):
31 """ filter(func, seq) """
32
33 if func and seq:
34 return ( filter(func, seq), )
35 else:
36 return ( [], )
116def dict_to_filter_params(d, prefix=''):
117 """
118 Translate a dictionary of attributes to a nested set of parameters suitable for QuerySet filtering. For example:
119
120 {
121 "name": "Foo",
122 "rack": {
123 "facility_id": "R101"
124 }
125 }
126
127 Becomes:
128
129 {
130 "name": "Foo",
131 "rack__facility_id": "R101"
132 }
133
134 And can be employed as filter parameters:
135
136 Device.objects.filter(**dict_to_filter(attrs_dict))
137 """
138 params = {}
139 for key, val in d.items():
140 k = prefix + key
141 if isinstance(val, dict):
142 params.update(dict_to_filter_params(val, k + '__'))
143 else:
144 params[k] = val
145 return params

Related snippets