Files
30-seconds-of-code/snippets/group_by.md
Angelos Chalaris ab3c44a5d5 Resolves #105
2019-09-22 15:21:54 +03:00

563 B

title, tags
title tags
group_by list,object,beginner

Groups the elements of a list based on the given function.

Use list() in combination with map() and fn to map the values of the list to the keys of an object. Use list comprehension to map each element to the appropriate key.

def group_by(lst, fn):
    return {key : [el for el in lst if fn(el) == key] for key in map(fn,lst)}
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']}