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

benedict.core.groupby   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A groupby() 0 12 5
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