benedict.core.groupby.groupby()   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 12
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 12
rs 9.3333
c 0
b 0
f 0
cc 5
nop 2
1
from benedict.utils import type_util
2
3
4
def groupby(items, key):
5
    if not type_util.is_list(items):
6
        raise ValueError("items should be a list of dicts.")
7
    items_grouped = {}
8
    for item in items:
9
        if not type_util.is_dict(item):
10
            raise ValueError("item should be a dict.")
11
        group = item.get(key)
12
        if group not in items_grouped:
13
            items_grouped[group] = []
14
        items_grouped[group].append(item.copy())
15
    return items_grouped
16