Total Complexity | 5 |
Total Lines | 18 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |