maximum element?

I

Ivan Voras

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... :)
 
D

David M. Cooke

At some point said:
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)
 
R

Robert Kern

Ivan said:
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
(e-mail address removed)

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

Ivan Voras

JCM said:
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 :)
 
T

Thorsten Kampe

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top