Thread: Python greatest number in the list

1. First Cup of Ubuntu
Join Date
Jul 2013
Beans
1

Python greatest number in the list

solved!
Last edited by mishumishu; July 9th, 2013 at 07:40 AM.

2. Re: Python greatest number in the list

Well, we are all very happy for you but what was the question?

3. Re: Python greatest number in the list

Originally Posted by ofnuts
Well, we are all very happy for you but what was the question?
let's try to guess the question and the answer:
the question:
given a list of numbers, how can I find the greatest of them in Python?
use
Code:
`max(your_list_of_numbers)`

4. Re: Python greatest number in the list

Besides the one good answer, there are several not-so-good ones (like sorted(list)[-1])

5. Re: Python greatest number in the list

Originally Posted by ofnuts
Code:
`sorted(list)[-1]`
this is a sample of a slightly obfuscated code, isn't it? Indeed, at least for me, the purpose of a code like this isn't at all easy to understand.

6. Tall Cafè Ubuntu
Join Date
Feb 2009
Beans
1,467

Re: Python greatest number in the list

Seems perfectly clear to me: take the last element of a sorted list.

7. Re: Python greatest number in the list

Next time I'll use 'reduce(lambda x,y : x if x >y else y,list)'

8. Re: Python greatest number in the 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)?"

9. Re: Python greatest number in the 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.

10. Re: Python greatest number in the list

This is why knowing the correct answer to questions like this is important:
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```
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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•