Python max function for "list"

Hi everyone,

 I am learning about " list"

Please consider the following example:

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

Hi @zeeshannetwork,

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 > LOO
LOO > KOO
KOO > 12
LOO > 1

Think about words in dictionary.

Hope this help :slight_smile:

-Gabriele-

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

2 Likes

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

LOO > WOO

should be:

LOO < WOO
2 Likes

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

1 Like