Sick Gaming
[Tut] How to Find the Max of List of Lists in Python? - 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] How to Find the Max of List of Lists in Python? (/thread-95026.html)



[Tut] How to Find the Max of List of Lists in Python? - xSicKxBot - 05-13-2020

How to Find the Max of List of Lists in Python?

<div><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>
<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><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’s the syntax:</p>
<p><code>max(iterable, key=None)</code></p>
<p><strong>Arguments:</strong></p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>iterable</code></td>
<td>The values among which you want to find the maximum. In our case, it’s a list of lists.</td>
</tr>
<tr>
<td><code>key</code></td>
<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>
</tr>
</tbody>
</table>
</figure>
<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 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/05/12/how-to-find-the-max-of-list-of-lists-in-python/