{"id":112808,"date":"2020-05-12T16:17:29","date_gmt":"2020-05-12T16:17:29","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8422"},"modified":"2020-05-12T16:17:29","modified_gmt":"2020-05-12T16:17:29","slug":"how-to-find-the-max-of-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/12\/how-to-find-the-max-of-list-of-lists-in-python\/","title":{"rendered":"How to Find the Max of List of Lists in Python?"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"How to Find the Max of List of Lists in Python?\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/Y1ZKmCfqCZk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p><strong>Problem<\/strong>: Say you have a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" target=\"_blank\">list of lists<\/a> (nested list) and you want to find the maximum of this list. It&#8217;s not trivial to compare lists&#8212;what&#8217;s the maximum among lists after all? To define the maximum among the inner lists, you may want to consider different objectives.<\/p>\n<ol>\n<li>The first element of each inner list.<\/li>\n<li>The i-th element of each inner list.<\/li>\n<li>The sum of inner list elements.<\/li>\n<li>The maximum of inner list elements.<\/li>\n<li>The minimum of inner list elements.<\/li>\n<\/ol>\n<p><strong>Example<\/strong>: Given list of lists <code>[[1, 1, 1], [0, 2, 0], [3, 3, -1]]<\/code>. Which is the maximum element? <\/p>\n<ol>\n<li>The first element of each inner list. The maximum is <code>[3, 3, -1]<\/code>. <\/li>\n<li>The i-th element of each inner list (<code>i = 2<\/code>). The maximum is <code>[1, 1, 1]<\/code>.<\/li>\n<li>The sum of inner list elements. The maximum is <code>[3, 3, -1]<\/code>.<\/li>\n<li>The maximum of inner list elements. The maximum is <code>[3, 3, -1]<\/code>.<\/li>\n<li>The minimum of inner list elements. The maximum is <code>[3, 3, -1]<\/code>.<\/li>\n<\/ol>\n<p>So how do you accomplish this?<\/p>\n<p><strong>Solution<\/strong>: Use the <code>max()<\/code> function with key argument.<\/p>\n<p><strong>Syntax<\/strong>: The <code>max()<\/code> function is a built-in function in Python (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-check-your-python-version\/\" target=\"_blank\">Python versions 2.x and 3.x<\/a>). Here&#8217;s the syntax:<\/p>\n<p><code>max(iterable, key=None)<\/code><\/p>\n<p><strong>Arguments:<\/strong><\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Argument<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>iterable<\/code><\/td>\n<td>The values among which you want to find the maximum. In our case, it&#8217;s a list of lists.<\/td>\n<\/tr>\n<tr>\n<td><code>key<\/code><\/td>\n<td>(Optional. Default <code>None<\/code>.) Pass a function that takes a single argument and returns a comparable value. The function is then applied to each element in the list. Then, the method find the maximum based on the key function results rather than the elements themselves.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Let&#8217;s study the solution code for our different versions of calculating the maximum &#8220;list&#8221; of a list of lists (nested list).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lst = [[1, 1, 1], [0, 2, 0], [3, 3, -1]] # Maximum using first element\nprint(max(lst, key=lambda x: x[0]))\n# [3, 3, -1] # Maximum using third element\nprint(max(lst, key=lambda x: x[2]))\n# [1, 1, 1] # Maximum using sum()\nprint(max(lst, key=sum))\n# [3, 3, -1] # Maximum using max\nprint(max(lst, key=max))\n# [3, 3, -1] # Maximum using min\nprint(max(lst, key=min))\n# [1, 1, 1]<\/pre>\n<p>Try it yourself in our interactive code shell:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@finxter\/maxlistoflists?lite=true\" scrolling=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\" width=\"100%\" height=\"800px\" frameborder=\"no\"><\/iframe> <\/p>\n<p><strong>Related articles:<\/strong> <\/p>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-methods\/\" target=\"_blank\">Python List Methods [Overview]<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-sort\/\" target=\"_blank\">Python List sort() &#8211; The Ultimate Guide<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\">Python Lists &#8211; Everything You Need to Know to Get Started<\/a><\/li>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-get-the-key-with-the-maximum-value-in-a-dictionary\/\" target=\"_blank\">Key with Maximum Value in Dict<\/a><\/li>\n<\/ul>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Say you have a list of lists (nested list) and you want to find the maximum of this list. It&#8217;s not trivial to compare lists&#8212;what&#8217;s the maximum among lists after all? To define the maximum among the inner lists, you may want to consider different objectives. The first element of each inner list. The [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-112808","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=112808"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112808\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}