{"id":4345,"date":"2020-03-28T11:10:15","date_gmt":"2020-03-28T11:10:15","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4345"},"modified":"2022-08-06T13:12:56","modified_gmt":"2022-08-06T13:12:56","slug":"python-max-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-max-method","title":{"rendered":"The Python max() Method"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In this tutorial, we are going to understand the use of <strong>the Python max() method<\/strong>. Basically, the Python <code>max()<\/code> method returns the maximum value among the set of passed values or the elements of the passed iterable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python max() Method<\/h2>\n\n\n\n<p>Below is the syntax for using the Python <code>max()<\/code> method to find the largest value in an <strong>iterable<\/strong>,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax(iterable, *&#x5B;, key, default])\n<\/pre><\/div>\n\n\n<p>Here, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>iterable<\/strong> is the object that contains the values for which the largest is to be found, <\/li><li><strong>key<\/strong> specifies the one-argument ordering function, <\/li><li>And the <strong>default<\/strong> is the default value returned by the method if the passed iterable is empty. <\/li><\/ul>\n\n\n\n<p>For finding largest among two or more values passed as arguments, <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax(arg1, arg2, *args&#x5B;, key]) \n<\/pre><\/div>\n\n\n<p>Here, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>arg1, arg2<\/strong>, &#8230;. <strong>argn<\/strong> are <strong>n<\/strong> values among which, the <code>max()<\/code> method would return the largest value, <\/li><li>And key again is the ordering function. <\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Working with the Python max() Method<\/h2>\n\n\n\n<p>We can use the <code>max()<\/code> method in various ways to find the maximum or largest of a given iterable or for two or more arguments. <\/p>\n\n\n\n<p>Let us see how the method works with an iterable object, two or more values, with specified key function and for multiple iterable objects passed as arguments. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">With iterable object<\/h2>\n\n\n\n<p>For the example below, we consider a <strong>list<\/strong> with some values in it for which we are to find the largest element. Look at the code given below carefully.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#initialisation of list\nlist1 = &#x5B; 1,3,4,7,0,4,8,2 ]\n\n#finding max element\nprint(&quot;max value is : &quot;, max(list1,default=0))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax value is :  8\n<\/pre><\/div>\n\n\n<p>As we can see, for the above code we initialize a list, <code>list1<\/code> and directly pass it to the <code>max()<\/code> method with the default value set to <strong>0<\/strong>. The function returns <strong>8<\/strong> as it is the largest value. <\/p>\n\n\n\n<p>If the list was <strong>empty<\/strong>, the function would have passed the default value, which is <strong>0<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Passing Two or More Values to the Python max() Method<\/h2>\n\n\n\n<p>When two or more values are passed to the <code>max()<\/code> method, it returns the maximum or largest of them all. These arguments can be integers, floating-point values, characters or even strings. <\/p>\n\n\n\n<p>Let us take an example,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;max value is : &quot;, max(6,1,73,6,38))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax value is :  73\n<\/pre><\/div>\n\n\n<p>As desired, we get the maximum value, <strong>73.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">With key function<\/h2>\n\n\n\n<p>As we <a href=\"https:\/\/www.askpython.com\/python\/list\/python-sort-list\" class=\"rank-math-link\">mentioned earlier<\/a>, the key is a one-lined ordering function on the basis of which the maximum value among a set of values is to be found.<\/p>\n\n\n\n<p>For example, if we want to find a tuple from a <strong>list of tuples<\/strong>, which has the largest value of the <strong>2nd<\/strong> element. Let us see how we can do that. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#initialisation of variables\nlist1 = &#x5B;(9,2,7), (6,8,4), (3,5,1)]\n\ndef f(tuple_1):\n    return tuple_1&#x5B;1]\n\nprint(&quot;max : &quot;, max(list1, key=f))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax :  (6, 8, 4)\n<\/pre><\/div>\n\n\n<p>Here, <code>f()<\/code> is a user-defined function that returns the <strong>2nd<\/strong> element of the passed tuple. Passing this function as a <strong>key<\/strong> to the <code>max()<\/code> method ensures that a tuple is returned with the largest 2nd element. For our example, it is <strong>( 6, 8, 4).<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Passing Multiple Iterables as Arguments<\/h2>\n\n\n\n<p>As we stated earlier, the Python max() method can also return the largest of multiple iterable elements as arguments. These arguments can be iterable like string, character, tuple, list or etc..<\/p>\n\n\n\n<p>By <strong>default<\/strong>, the <code>max()<\/code> method returns the object with the maximum <strong>0th<\/strong> element for lists, tuples, etc. And for strings, it compares the first character of each string passed.<\/p>\n\n\n\n<p>Below we have taken an example for tuples. Look at the code carefully. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#initialisation of variables\ntuple1 = (5,23,7)\ntuple2 = (4,1,7)\ntuple3 = (7,37,1)\n\nprint(&quot;max : &quot;, max(tuple1,tuple2,tuple3))\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmax :  (7, 37, 1)\n<\/pre><\/div>\n\n\n<p>For this example, <strong>three<\/strong> tuples with some initial values have been directly passed to the <code>max()<\/code> method. Which returns the tuple with the largest first element i.e., <strong>(7, 37, 1)<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So in this tutorial, we learned about the Python <code>max()<\/code> method, its uses as well as its working. <\/p>\n\n\n\n<p>Please remember, that if the <strong>default<\/strong> value is not set and an empty iterable is passed as arguments to the <code>max()<\/code> function raises a <strong>ValueError<\/strong>.<\/p>\n\n\n\n<p>For any further questions related to this topic feel free to use the comments below. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python max() &#8211; Journal Dev Post,<\/li><li><a aria-label=\"max() (opens in a new tab)\" href=\"https:\/\/docs.python.org\/3.7\/library\/functions.html#max\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">max()<\/a> &#8211; Python Documentation,<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/18296755\/python-max-function-using-key-and-lambda-expression\" target=\"_blank\" rel=\"noopener\">python max function using &#8216;key&#8217; and lambda expression<\/a> &#8211; Stack Overflow Question.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we are going to understand the use of the Python max() method. Basically, the Python max() method returns the maximum value among the set of passed values or the elements of the passed iterable. Using the Python max() Method Below is the syntax for using the Python max() method to find [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":4350,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4345"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4345\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4350"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}