Skip to content

Instantly share code, notes, and snippets.

@jmg
Created April 29, 2015 15:42
Show Gist options
  • Select an option

  • Save jmg/669a7d42801aae97bbff to your computer and use it in GitHub Desktop.

Select an option

Save jmg/669a7d42801aae97bbff to your computer and use it in GitHub Desktop.
slice objects in sublists sharing a common propery
def slice(objects, prop):
"""
slice objects in sublists sharing a common propery
"""
if not objects:
return []
def get_prop(obj, prop):
if not isinstance(objects[0], dict):
return obj.__dict__[prop]
return obj[prop]
set_objects = set([get_prop(o, prop) for o in objects])
return [[o for o in objects if get_prop(o, prop) == obj] for obj in set_objects]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment