Reputation: 65
I have the following list
[<QuerySet [{'number': 123}]>, <QuerySet []>, <QuerySet []>, <QuerySet [{'number': 21323}]>, <QuerySet []>]
I want to extract the numbers in an other list with a for loop and skip the empty QuerySets, but was not successful yet. Is there an easy way?
Tahnk you
Upvotes: 1
Views: 64
Reputation: 476557
You can work with:
[q['number'] for qs in qs_list for q in qs]
where qs_list
is the list of QuerySet
s.
That being said, often a list of QuerySet
s means you are doing something inefficient, since you will here make a query per QuerySet
in the list. Often you can derive the values all in the same query.
Upvotes: 1