Returns two dictionaries, one containing all the items whose keys do not start with a prefix and another containing all the items whose keys do start with the prefix. Note that the prefix is not removed from the keys.
(d, prefix)
| 273 | |
| 274 | |
| 275 | def split_dict_by_key_prefix(d, prefix): |
| 276 | """ |
| 277 | Returns two dictionaries, one containing all the items whose keys do not |
| 278 | start with a prefix and another containing all the items whose keys do start |
| 279 | with the prefix. Note that the prefix is not removed from the keys. |
| 280 | """ |
| 281 | no_prefix = dict() |
| 282 | with_prefix = dict() |
| 283 | for k in d.keys(): |
| 284 | if k.startswith(prefix): |
| 285 | with_prefix[k] = d[k] |
| 286 | else: |
| 287 | no_prefix[k] = d[k] |
| 288 | return (no_prefix, with_prefix) |