Velocity Reviews > maximum element?

# maximum element?

Ivan Voras
Guest
Posts: n/a

 03-04-2004
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...

David M. Cooke
Guest
Posts: n/a

 03-04-2004
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

Heather Coppersmith
Guest
Posts: n/a

 03-04-2004
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...

Regards,
Heather

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

Robert Kern
Guest
Posts: n/a

 03-04-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

JCM
Guest
Posts: n/a

 03-04-2004
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.

Ivan Voras
Guest
Posts: n/a

 03-05-2004
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

Thorsten Kampe
Guest
Posts: n/a

 04-28-2004
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