maximum() efficency

Steve R. Hastings steve at hastings.org
Sun Mar 26 18:35:51 EST 2006


On Sun, 26 Mar 2006 20:34:28 +0200, Mitja Trampus wrote:
> I would have done it in the same way, but probably without the iterators. I.e., like this:
> 
> def maximum(lst):
> 	try: m = lst[0]
> 	except (TypeError, IndexError): raise Exception "Non-sequence or empty sequence given to 
> maximum")
> 
> 	# (you forgot the above sanity checks in your code.
> 	# not strictly necessary, but nice to have.)
> 
> 	for x in lst:
> 		if x>m: m=x
> 	return m

I left out the original sanity check, because I just wanted a streamlined
simple example.


I had a nagging feeling that I was missing something simple, and you have
put your finger on it.  That's perfect!  It's simple, it's clear, and it
will work on any version of Python.  Thanks!
-- 
Steve R. Hastings    "Vita est"
steve at hastings.org    http://www.blarg.net/~steveha




More information about the Python-list mailing list