# Function to determine list max without itertools

Sayth Renshaw flebber.crue at gmail.com
Thu Apr 18 00:10:46 EDT 2019

```wrote:
> >
> >
> > I have created a function that takes a list as an argument.
> > Without using itertools I want to compare each item in the list to find the max.
> >
> > However instead of the max I keep getting the  last item in the list. Where is my logic wrong here?
> >
> > def maximum(listarg):
> >     items = list(listarg)
> >     myMax = 0
> >     for index, item in enumerate(items):
> >         for otheritem in items[index + 1 :]:
> >             if item < otheritem:
> >                 myMax = otheritem
> >             elif item > otheritem:
> >                 myMax = item
> >             else:
> >                 myMax = myMax
> >
> > Seems like it should work but doesn't.
> >
>
> debugging your actual code. You should be able to find the largest in
> a collection without making many many passes over the items - a single
> pass should be sufficient.
>
> ChrisA

Most I am finding either use the max function or itertools. Both of which I am trying not to use so that I actually do it myself.

This one on SO is where I was going https://stackoverflow.com/a/3990826/461887

def maxelements(seq):
''' Return list of position(s) of largest element '''
max_indices = []
if seq:
max_val = seq[0]
for i,val in ((i,val) for i,val in enumerate(seq) if val >= max_val):
if val == max_val:
max_indices.append(i)
else:
max_val = val
max_indices = [i]

return max_indices

This is probably the nicest one but still uses Max.

>>> a=[5,4,3,2,1]
>>> def eleMax(items, start=0, end=None):
...     return max(items[start:end])

Thanks

Sayth
```