# maximum element?

Discussion in 'Python' started by Ivan Voras, Mar 4, 2004.

1. ### Ivan VorasGuest

What is the 'most pythonic' way of searching the largest element in a
list/tuple?

My 'standard' idea is:

max = list[0] # or -infinity, or whatever...
for i in list:
if i > max:
max = i

While this is ok, I somehow 'feel' there could be a more concise
solution...

Ivan Voras, Mar 4, 2004

2. ### David M. CookeGuest

At some point, Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:

> What is the 'most pythonic' way of searching the largest element in a
> list/tuple?
>
> My 'standard' idea is:
>
> max = list[0] # or -infinity, or whatever...
> for i in list:
> if i > max:
> max = i
>
> While this is ok, I somehow 'feel' there could be a more concise
> solution...

max(list)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca

David M. Cooke, Mar 4, 2004

3. ### Heather CoppersmithGuest

On Thu, 04 Mar 2004 21:52:50 +0100,
Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:

> What is the 'most pythonic' way of searching the largest element in a
> list/tuple?

> My 'standard' idea is:

> max = list[0] # or -infinity, or whatever...
> for i in list:
> if i > max:
> max = i

> While this is ok, I somehow 'feel' there could be a more concise
> solution...

How about max( list )?

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli

Heather Coppersmith, Mar 4, 2004
4. ### Robert KernGuest

Ivan Voras wrote:
> What is the 'most pythonic' way of searching the largest element in a
> list/tuple?

max(list)

> My 'standard' idea is:
>
> max = list[0] # or -infinity, or whatever...
> for i in list:
> if i > max:
> max = i
>
> While this is ok, I somehow 'feel' there could be a more concise
> solution...

Indeed.

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Mar 4, 2004
5. ### JCMGuest

Ivan Voras <ivoras@__geri.cc.fer.hr> wrote:
> What is the 'most pythonic' way of searching the largest element in a
> list/tuple?

> My 'standard' idea is:

> max = list[0] # or -infinity, or whatever...
> for i in list:
> if i > max:
> max = i

> While this is ok, I somehow 'feel' there could be a more concise
> solution...

reduce(lambda x, y: (x, y)[x < y], list)

Sorry, couldn't resist.

JCM, Mar 4, 2004
6. ### Ivan VorasGuest

JCM wrote:

> reduce(lambda x, y: (x, y)[x < y], list)
>
> Sorry, couldn't resist.

Actually, I thought that the 'better' way would be using a lambda
function but then again, I was waaay wrong ))

Thanks, all

Ivan Voras, Mar 5, 2004
7. ### Thorsten KampeGuest

Catching up...

* Ivan Voras (2004-03-04 22:52 +0100)
> What is the 'most pythonic' way of searching the largest element in a
> list/tuple?
>
> My 'standard' idea is:
>
> max = list[0] # or -infinity, or whatever...
> for i in list:
> if i > max:
> max = i
>
> While this is ok, I somehow 'feel' there could be a more concise
> solution...

max(seq)

In a real world example you wouldn't be interested in "the" maximum
but in the extrema/maxima according to function f (in your case the
identity f(x)=x), which could be more than just one item.

Thorsten

Thorsten Kampe, Apr 28, 2004