Reputation: 321
I have two variables:
var1 ex: [6, 2, 4, 5] type: ValuesListQuerySet
var2 ex: ['4', '2'] type: List
I must compare the elements of these two variables , the result should be:
['4', '2']
My code:
idmatch = []
for r in var1:
for k in var2:
if k == r:
print("here")
idmatch.append(str(k))
The two elements are never the same , and the result is:
[]
How can i compare them?
Thank you
Upvotes: 0
Views: 304
Reputation: 309029
Your ValuesListQuerySet
contains integers, but your list contains strings.
If you are not concerned about the order, the easiest solution is to convert both variables to sets.
# convert var1 to a set of strings
var1 = set(str(x) for x in var1)
# convert var2 to a set
var2 = set(var2)
idmatch = var1 & var2
Upvotes: 2
Reputation: 599846
The difference is not queryset vs list, but the content of those; one has integers, the other has strings.
Upvotes: 2