On 13/04/16 20:41, marcus lütolf wrote: > Hello experts > > I'am working exercise 5. of 'Practical Programming 2nd edition, .....using > Python 3' (operations on lists). > The following code get's me wrong results: > >>>> metals = [['beryllium', 4],['magnesium', 12], ['calcium', 20], >>>> ['strontium', 38], ['barium', 56], ['radium', 88]] >>>> max(metals) > ['strontium', 38] >>>> min(metals) > ['barium', 56] > > It should return > ['radium', 88] and > ['beryllium', 4] respectively
Why should it return radium as the max? It may be obvious to you but looking at it as simple data (which is all Python sees) that's not obvious at all. Why would radium come before strontium? And obviously(!) the first list element takes priority over the second when looking at simple data. (And Python just sees a collection of string/integer pairs, it has no idea what those strings/integers mean) If you want to order by the second element there are several things you can do, the simplest is probably to just swap the elements around in your tuples... But that might not be possible (or easy) depending on how you get the tuples. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor