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

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

 

 

 

 

 

3 Replies
Highlighted
Commander Lvl 2
Message 2 of 4

Re: Python max function for "list"

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 :)

 

-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 1
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

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

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