From ab3c44a5d511e4a2b389b94ef4d9ada0660ba6d3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 22 Sep 2019 15:21:54 +0300 Subject: [PATCH] Resolves #105 --- snippets/group_by.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/snippets/group_by.md b/snippets/group_by.md index 06147a45d..6bd8ec4d5 100644 --- a/snippets/group_by.md +++ b/snippets/group_by.md @@ -10,14 +10,11 @@ Use list comprehension to map each element to the appropriate `key`. ```py def group_by(lst, fn): - groups = {} - for key in list(map(fn,lst)): - groups[key] = [item for item in lst if fn(item) == key] - return groups + return {key : [el for el in lst if fn(el) == key] for key in map(fn,lst)} ``` ```py import math -group_by([6.1, 4.2, 6.3], math.floor); # {4: [4.2], 6: [6.1, 6.3]} -group_by(['one', 'two', 'three'], len); # {3: ['one', 'two'], 5: ['three']} +group_by([6.1, 4.2, 6.3], math.floor) # {4: [4.2], 6: [6.1, 6.3]} +group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']} ```