Function to determine list max without itertools

MRAB python at mrabarnett.plus.com
Thu Apr 18 07:48:51 EDT 2019


On 2019-04-18 08:39, Sayth Renshaw wrote:
> Thank you for the advice everyone.
> 
>> 
>> The first thing to try is find every place where you update myMax, and
> 
> This was actually where I was going wrong. I was setting max but then overwriting it with item. Then kept checking item only to return myMax.
> 
> I went looking for other solutions as I thought I must be well off the path in the shrubs but I was actually close.
> 
> This is how I ended up. There may be better solutions but this works.
> 
> def maximum(listarg):
>      items = list(listarg)
>      myMax = items[0]
>      for item in items:
>          for i in items[items.index(item)+1:len(items)]:
>              if myMax < i:
>                  myMax = i
>              else:
>                  pass
>              
>      return myMax
>        
It's still overly complicated.

>                  
> if __name__ == "__main__":
>      print(maximum([4,3,6,2,1,4]))
> 


More information about the Python-list mailing list