Sick Gaming
[Tut] Python List max() - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python List max() (/thread-96000.html)



[Tut] Python List max() - xSicKxBot - 07-02-2020

Python List max()

<div><p>Do you want to find the maximum of a Python list? This article gives you everything you need to know to master the <strong>max()</strong> function in Python. </p>
<h3>Description</h3>
<p>Python’s built-in <strong>max()</strong> function returns the maximum element of a list or its generalization (an iterable). </p>
<h3>Syntax</h3>
<p>The syntax of the <strong>max()</strong> function is as follows:</p>
<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="">max(list, default=obj, key=func)</pre>
<h3>Arguments</h3>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>list</code></td>
<td>The list or iterable from which you get the maximum value.</td>
</tr>
<tr>
<td><code>default</code></td>
<td>If the iterable is empty, returns this default object.</td>
</tr>
<tr>
<td><code>key</code></td>
<td>A function that associated a weight to each element in the iterable based on which the maximum is calculated.</td>
</tr>
</tbody>
</table>
</figure>
<h3>Return Value</h3>
<p>The return value of the <code>max(list)</code> function is a single element from the <code>list</code> that is the maximum of all elements. If the list is empty, the default object is returned, if explicitly defined. If not, a <code>ValueError</code> is thrown.</p>
<h3>Examples</h3>
<p>Let’s consider four examples that show how the arguments play together:</p>
<p> <iframe src="https://trinket.io/embed/python/7f277ce430" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Guess the output of the four print statements in the code. Run the code. How many did you guess right?</em></p>
<h2>Maximum of List of Lists</h2>
<p><em><a href="https://blog.finxter.com/how-to-find-the-max-of-list-of-lists-in-python/" target="_blank" rel="noreferrer noopener" title="How to Find the Max of a List of Lists in Python?">Original article: How to Find the Maximum of a List of Lists?</a></em></p>
<p>To find the maximal list in a list of lists, you need to make two lists comparable. How? With the <code>key</code> argument of the <code>max()</code> function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the largest numerical value is returned as the maximum of the list of lists.</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<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’s not trivial to compare lists—what’s the maximum among lists after all? To define the maximum among the inner lists, you may want to consider different objectives.</p>
<ol>
<li>The first element of each inner list.</li>
<li>The i-th element of each inner list.</li>
<li>The sum of inner list elements.</li>
<li>The maximum of inner list elements.</li>
<li>The minimum of inner list elements.</li>
</ol>
<figure class="wp-block-image size-large is-resized"><img src="https://blog.finxter.com/wp-content/uploads/2020/05/max-1024x576.jpg" alt="" class="wp-image-8472" width="768" height="432" srcset="https://blog.finxter.com/wp-content/uploads/2020/05/max-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/05/max-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/05/max-768x432.jpg 768w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
<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>
<ol>
<li>The first element of each inner list. The maximum is <code>[3, 3, -1]</code>. </li>
<li>The i-th element of each inner list (<code>i = 2</code>). The maximum is <code>[1, 1, 1]</code>.</li>
<li>The sum of inner list elements. The maximum is <code>[3, 3, -1]</code>.</li>
<li>The maximum of inner list elements. The maximum is <code>[3, 3, -1]</code>.</li>
<li>The minimum of inner list elements. The maximum is <code>[3, 3, -1]</code>.</li>
</ol>
<p>So how do you accomplish this?</p>
<p><strong>Solution</strong>: Use the <code>max()</code> function with key argument.</p>
<p>Let’s study the solution code for our different versions of calculating the maximum “list” of a list of lists (nested list).</p>
<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
print(max(lst, key=lambda x: x[0]))
# [3, 3, -1] # Maximum using third element
print(max(lst, key=lambda x: x[2]))
# [1, 1, 1] # Maximum using sum()
print(max(lst, key=sum))
# [3, 3, -1] # Maximum using max
print(max(lst, key=max))
# [3, 3, -1] # Maximum using min
print(max(lst, key=min))
# [1, 1, 1]</pre>
<p>Try it yourself in our interactive code shell:</p>
<p> <iframe 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>
<p><strong>Related articles:</strong> </p>
<ul>
<li><a href="https://blog.finxter.com/how-to-find-the-max-of-list-of-lists-in-python/" target="_blank" rel="noreferrer noopener" title="How to Find the Max of a List of Lists in Python?">How to Find the Maximum of a List of Lists in Python?</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-find-the-minimum-of-a-list-of-lists-in-python/" target="_blank">How to Find the Minimum of a List of Lists in Python?</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-methods/" target="_blank">Python List Methods [Overview]</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-sort/" target="_blank">Python List sort() – The Ultimate Guide</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">Python Lists – Everything You Need to Know to Get Started</a></li>
<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>
</ul>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/07/01/python-list-max/