gadss
gadss

Reputation: 22489

how to compare values in loop in python

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 ...

Upvotes: 0

Views: 190

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90742

You could use the min function.

min(ProdPrice.objects.filter(product=x['id']).aggregate(Min('price'))['price__min'] for x in c)

Upvotes: 5

Related Questions