Last edited by mishumishu; July 9th, 2013 at 07:40 AM.
Well, we are all very happy for you but what was the question?
Besides the one good answer, there are several not-so-good ones (like sorted(list)[-1])
Seems perfectly clear to me: take the last element of a sorted list.
Next time I'll use 'reduce(lambda x,y : x if x >y else y,list)'
what I meant to say in my previous post is: "sorting a list hasn't always the purpose of finding its maximum value; why should I use sorted(list[-1]) when it's much more understandable max(list)?"
My original question was really wether the OP found the right solution (max(list)), or stumbled on something else that does the job but isn't 100% correct (like the ones I mentioned). Since he didn't tell us, we don't know.
This is why knowing the correct answer to questions like this is important:
Time differences like these stack up very quickly in business applications that handle hundreds or even thousands of queries per second. It is important to know the fastest way to handle trivial operations like this so you don't drive yourself nuts when trying to optimize your code later.Code:l = list(range(1000)); random.shuffle(l) max(l): 0.105857849121 milliseconds sorted(l)[-1]: 0.613212585449 milliseconds reduce(lambda x, y : x if x > y else y, l): 1.07598304749 milliseconds