Function to determine list max without itertools

Sayth Renshaw flebber.crue at
Fri Apr 19 06:01:06 EDT 2019

> Now, what happens when the code is tested with various (different) sets 
> of test-data?
> (remember the last question from my previous msg!?)
It fails on any list entry that isn't a float or an int, giving a TypeError.

> Once you are happy with the various test-runs, do you have any ideas for 
> making the code more efficient?
> -- 
> Regards =dn

Efficient No, but resistant to error I can buy using try except to pass through TypeErrors.

def max_try(listarg):
    myMax = listarg[0]
        for item in listarg:
            if item > myMax:
                myMax = item
    except TypeError:
        print(f'Only numbers are supported, this entry "{item}" was not')

    return myMax


PS Since I am going through the list fully the only optimisation I can think of a generator to feed it seems sort of redundant. Unless of course it was a list of 10,000 numbers then maybe its useful. 

Maybe a generator with a condition that if list length is greater than 500 to chunk it into 250 lengths. 

But then If we go along this path then you could say if every other task in an application waited on it could be very important so then async await might be an option.


More information about the Python-list mailing list