Chalarangelo/30-seconds-of-python

[FEATURE] replace for loop in group_by with dictionary comprehension

defterade opened this issue · 1 comments

Category:

Description

Also removes unnecessary list cast.

There are also unnecessary semicolons in the examples section of this function.

Current Snippet Behavior

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

Possible Solution

def group_by(lst, fn):
    return {key : [el for el in lst if fn(el) == key] for key in map(fn,lst)}
lock commented

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.