Help
cancel
Showing results for
Did you mean:
Ground Controller Lvl 1
Message 1 of 4

## Python max function for "list"

Hi everyone,

I am learning about " list"

j=['1', '12', 'KOO', 'LOO']

If we do :

>>> max (j)
'LOO'

How is max being determined above?  I know it is a dumb example but I am just curious what Python thinks it is larger  value in the list .

Thanks

3 Replies
Highlighted
Commander Lvl 2
Message 2 of 4

## Re: Python max function for "list"

Welcome to the Github Community Forum.

It's a little tricky to understand it but the reason is because order in a list of string is given by the lexicographical order, if you open a dictionary you will understand this concept very well. For example take this list of string:

`j = ['1', '11', '234', 'a']`

the max element of this is a and the minimum element is 1, this because 1<a and 234<a.

You can try add some more element and change the order to this list:

`j = ['a', 'bz', 'z', 'zzzz', '\$', '11', '234']`

Here the minimum element is \$ and the maximum is zzzz.

You can see the exact order using the sort method:

```j = ['a', 'bz', 'z', 'zzzz', '\$', '11', '234']
print(sort(j))```

the output will be:

`['\$', '11', '234', 'a', 'bz', 'z', 'zzzz']`

To be precise i give some more example:

```bed > animal
zap > bed
animal > 123456789
123456789 > \$WOO > LOOLOO > KOO
KOO > 12LOO > 1```

Hope this help :)

-Gabriele-

Mark helpfull posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great contents!

Commander Lvl 2
Message 3 of 4

## Re: Python max function for "list"

Good explanation. Minor typo in the last example I think:

`LOO > WOO`

should be:

`LOO < WOO`

Commander Lvl 2
Message 4 of 4

## Re: Python max function for "list"

Yes @fire-eggs you're right, I wanted to mention the example of @zeeshannetwork but I miswrote. I'm going to edit and add that on the list.

Thank you

New solutions