Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.core.groupby.groupby()   A

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
# -*- coding: utf-8 -*-
2
3
from benedict.utils import type_util
4
5
6
def groupby(items, key):
7
    if not type_util.is_list(items):
8
        raise ValueError('items should be a list of dicts.')
9
    items_grouped = {}
10
    for item in items:
11
        if not type_util.is_dict(item):
12
            raise ValueError('item should be a dict.')
13
        group = item.get(key)
14
        if group not in items_grouped:
15
            items_grouped[group] = []
16
        items_grouped[group].append(item.copy())
17
    return items_grouped
18