Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting list of python dictionaries by repeating dictionary key values

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'},
]
like image 241
atman Avatar asked May 28 '13 05:05

atman


2 Answers

>>> 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'}]]
like image 79
jamylak Avatar answered Oct 08 '22 18:10

jamylak


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))
like image 35
qwwqwwq Avatar answered Oct 08 '22 18:10

qwwqwwq