[Answer]-Python- Use set on a List of objects <class 'xyz.models.abc'>

0👍

✅

result = []
seen = set()
for obj in objects:
    if obj.field1 not in seen:
        seen.add(obj.field1)
        result.append(obj)

1👍

Is your “list” of django objects a queryset? If so a quicker, less memory intensive approach would be to run a distinct query

objects.order_by('field1').distinct('field1')
  • Please note, this only works with PostgreSQL

0👍

I took a different approach using a dictionary comprehension.

unique_objs = {obj.field1:obj for obj in objects}.values()

Leave a comment