Skip to content

Home

Group list elements

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

from collections import defaultdict
from math import floor

def group_by(lst, fn):
  d = defaultdict(list)
  for el in lst:
    d[fn(el)].append(el)
  return dict(d)

group_by([6.1, 4.2, 6.3], floor) # {4: [4.2], 6: [6.1, 6.3]}
group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']}

More like this

Start typing a keyphrase to see matching snippets.