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
- [Answer]-How Do I Get Unique IDs Efficiently in Python?
- [Answer]-User connections in django
- [Answer]-Django: Make a query inside a class
0
I took a different approach using a dictionary comprehension.
unique_objs = {obj.field1:obj for obj in objects}.values()
- [Answer]-Variant data type in DB
- [Answer]-How to write helper method for PATCH request using django-tastypie?
- [Answer]-Django does a useless join when filtering
Source:stackexchange.com