поделить словарь на две части - всеядная версия

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def split(src, keys):
"""
>>> split({1:2, 3:4, 5:6, 7:8}, (1,3))
({1: 2, 3: 4}, {5: 6, 7: 8})
>>> split(((1, 2), (3, 4), (5, 6), (7, 8)), (1,3))
({1: 2, 3: 4}, {5: 6, 7: 8})
"""
have, dont = {}, {}
items = src.items() if hasattr(src, "items") else src
for key, value in items:
(have if key in keys else dont)[key] = value
return have, dont