say I have a list of dictionaries:
foo = [
{'host': 'localhost', 'db_name': 'test', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'test', 'table': 'users'},
{'host': 'localhost', 'db_name': 'test', 'table': 'sales'},
{'host': 'localhost', 'db_name': 'new', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'new', 'table': 'users'},
{'host': 'localhost', 'db_name': 'new', 'table': 'sales'},
]
How can I split this list into separate lists (or into a list of lists) where 'host' and 'db_name' are the same? For example:
list1 = [
{'host': 'localhost', 'db_name': 'test', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'test', 'table': 'users'},
{'host': 'localhost', 'db_name': 'test', 'table': 'sales'},
]
list2 = [
{'host': 'localhost', 'db_name': 'new', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'new', 'table': 'users'},
{'host': 'localhost', 'db_name': 'new', 'table': 'sales'},
]
>>> from collections import defaultdict
>>> dd = defaultdict(list)
>>> foo = [
{'host': 'localhost', 'db_name': 'test', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'test', 'table': 'users'},
{'host': 'localhost', 'db_name': 'test', 'table': 'sales'},
{'host': 'localhost', 'db_name': 'new', 'table': 'partners'},
{'host': 'localhost', 'db_name': 'new', 'table': 'users'},
{'host': 'localhost', 'db_name': 'new', 'table': 'sales'},
]
>>> for d in foo:
dd[(d['host'], d['db_name'])].append(d)
The lists of lists is the dictionary's values
>>> dd.values()
[[{'table': 'partners', 'host': 'localhost', 'db_name': 'new'}, {'table': 'users', 'host': 'localhost', 'db_name': 'new'}, {'table': 'sales', 'host': 'localhost', 'db_name': 'new'}], [{'table': 'partners', 'host': 'localhost', 'db_name': 'test'}, {'table': 'users', 'host': 'localhost', 'db_name': 'test'}, {'table': 'sales', 'host': 'localhost', 'db_name': 'test'}]]
This is a perfect use case for the groupby
function from itertools
:
from itertools import groupby
foo.sort(key = lambda x: (x['db_name'], x['host']))
it = groupby(foo, key = lambda x: (x['db_name'], x['host']) )
groups = []
keys = []
for k, g in it:
groups.append(list(g))
keys.append(k)
print groups
## >>>
##[
## [{'table': 'partners', 'host': 'localhost', 'db_name': 'test'},
## {'table': 'users', 'host': 'localhost', 'db_name': 'test'},
## {'table': 'sales', 'host': 'localhost', 'db_name': 'test'}],
## [{'table': 'partners', 'host': 'localhost', 'db_name': 'new'},
## {'table': 'users', 'host': 'localhost', 'db_name': 'new'},
## {'table': 'sales', 'host': 'localhost', 'db_name': 'new'}]
##]
##or make a dict
d = dict(zip(keys, groups))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With