4

I have to merge the querysets below in a single list:

result_list_1 = Col.objects.defer("tags").filter(producer__username__icontains=crit) 
result_list_2 = Col.objects.defer("tags").filter(name__icontains=crit)
result_list_3 = Col.objects.defer("tags").filter(description__icontains=crit)
result_list_4 = Col.objects.filter(tags__name__icontains=crit)
...

Each result_list contains items, which have a unique numeric id I can use to make sure there are no dups.

I can't use | while querying the DB or Q objects.

How do I merge the resulsets in one single list?

7
  • See here: stackoverflow.com/questions/431628/… Commented May 19, 2014 at 22:49
  • Thanks Alex, I saw that one. There is a reference in a comment about merging elements without duplicates, but it is not very clear. Commented May 19, 2014 at 22:58
  • Thanks Steinar, no Q objects. Commented May 19, 2014 at 22:59
  • But why? If you give us some context to your problem it might be easier to provide you with a solution. Commented May 19, 2014 at 23:00
  • The code sample above shows only 4 querysets, but I actually have a lot more, some of them with way more complex filtering criteria. They cannot be combined. I have to merge the resulting querysets. Commented May 19, 2014 at 23:09

2 Answers 2

5

What about a slight modification of itertools.chain that makes sure you don't get dupes:

def unique_chain(*iterables):
    known_ids = set()
    for it in iterables:
        for element in it:
            if element.id not in known_ids:
                known_ids.add(element.id)
                yield element

With that you can create your combined list:

combined_list = list(unique_chain(result_list_1, result_list_2, ... ))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Steffens. That did the trick. I suspect there might be more efficient methods but this one works.
I hate to rely on this technique but for quick prototyping, it saved my day. Now I just need to find an efficient applicable solution. Yeah its time complexity that takes my time But thank you
3

Then you can't have a QuerySet and it's django-related functionality, but you can use itertools.chain which is specifically for merging multiple iterables.

import itertools

merged = itertools.chain(qs1, qs2, qs3, qs4)

for element in merged:
    print(element)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.