0

how can i compare values in a loop? i just want to compare all the values if who is the smallest (minimum) value using loop.

this is my code:

>>> c = Product.objects.filter(client=1).values('id')
>>> c
[{'id': 2}, {'id': 1}, {'id':5}, {'id':8}]
>>> for x in c:
...  price = ProdPrice.objects.filter(product=x['id']).aggregate(Min('price'))['price__min']
...  print price
... 
1000.0
1050.0
900.0
3000.0
>>> 

in my code, i just want to compare all the products and print only the smallest/minimum price using loop.

i just want to print is 900.0 the smallest price of all the products.

thus anyone have an idea about my situation?

thanks in advance ...

1
  • 1
    If you want to find the smallest value, then say you want to find the smallest value. Expecting to need a loop is only an assumption on your part. Also, I have no idea what this is supposed to do with django... Commented Feb 25, 2012 at 10:49

1 Answer 1

5

You could use the min function.

min(ProdPrice.objects.filter(product=x['id']).aggregate(Min('price'))['price__min'] for x in c)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.