Sick Gaming
[Tut] Python Find Shortest List in List - 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 Find Shortest List in List (/thread-99985.html)



[Tut] Python Find Shortest List in List - xSicKxBot - 09-25-2022

Python Find Shortest List in List

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;713706&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem Formulation</h2>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Programming Challenge</strong>: Given a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">list of lists</a> (nested list). Find and return the shortest inner list from the outer list of lists. </p>
<p>Here are some examples:</p>
<ul>
<li><code>[[1], [2, 3], [4, 5, 6]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>[1]</code></li>
<li><code>[[1, [2, 3], 4], [5, 6], [7]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>[7]</code></li>
<li><code>[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>[[1], [2], [3]]</code></li>
</ul>
<p>Also, you’ll learn how to solve a variant of this challenge.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Bonus challenge</strong>: Find only the <em>length </em>of the shortest list in the list of lists.</p>
<p>Here are some examples:</p>
<ul>
<li><code>[[1], [2, 3], [4, 5, 6]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>1</code></li>
<li><code>[[1, [2, 3], 4], [5, 6], [7]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>1</code></li>
<li><code>[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]</code> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code>3</code></li>
</ul>
<p>So without further ado, let’s get started!</p>
<h2>Method 1: min(lst, key=len)</h2>
<p class="has-global-color-8-background-color has-background">Use Python’s built-in <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-min/" data-type="URL" data-id="https://blog.finxter.com/python-min/" target="_blank">min()</a></code> function with a key argument to find the shortest list in a list of lists. Call <code>min(lst, key=len)</code> to return the shortest list in <code>lst</code> using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-built-in-functions/" target="_blank">built-in</a> <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-len/" target="_blank">len()</a></code> function to associate the weight of each list, so that the shortest inner list will be the minimum.</p>
<p>Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_shortest_list(lst): return min(lst, key=len) print(get_shortest_list([[1], [2, 3], [4, 5, 6]]))
# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [[1], [2], [3]]
</pre>
<p>A beautiful <a rel="noreferrer noopener" href="https://pythononeliners.com/" data-type="URL" data-id="https://pythononeliners.com/" target="_blank">one-liner solution</a>, isn’t it? <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Let’s have a look at a slight variant to check the <em>length </em>of the shortest list instead.</p>
<h2>Method 2: len(min(lst, key=len))</h2>
<p class="has-global-color-8-background-color has-background">To get the length of the shortest list in a <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890">n</a><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank">e</a><a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890">sted list</a>, use the <code>len(min(lst, key=len))</code> function. First, you determine the shortest inner list using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-min/" data-type="URL" data-id="https://blog.finxter.com/python-min/" target="_blank">min()</a></code> function with the key argument set to the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-len/" data-type="post" data-id="22386" target="_blank">len()</a></code> function. Second, you pass this shortest list into the <code>len()</code> function itself to determine the minimum.</p>
<p>Here’s an analogous example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_length_of_shortest_list(lst): return len(min(lst, key=len)) print(get_length_of_shortest_list([[1], [2, 3], [4, 5, 6]]))
# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 3
</pre>
<h2>Method 3: min(len(x) for x in lst)</h2>
<p class="has-global-color-8-background-color has-background">A Pythonic way to check the length of the shortest list is to combine a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-use-generator-expressions-in-python-dictionaries/" data-type="post" data-id="7502" target="_blank">generator expression</a> or <a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171">list comprehension</a> with the <code>min()</code> function without key. For instance, <code>min(len(x) for x in lst)</code> first turns all inner list into length integer numbers and passes this iterable into the <code>min()</code> function to get the result.</p>
<p>Here’s this approach on the same examples as before:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_length_of_shortest_list(lst): return min(len(x) for x in lst) print(get_length_of_shortest_list([[1], [2, 3], [4, 5, 6]]))
# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 3
</pre>
<p>A good training effect can be obtained by studying the following tutorial on the topic—feel free to do so!</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Training</strong>: <a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">Understanding List Comprehension in Python</a></p>
<h2>Method 4: Naive For Loop</h2>
<p class="has-global-color-8-background-color has-background">A not so Pythonic but still fine approach is to iterate over all lists in a <code>for</code> loop, check their length using the <code>len()</code> function, and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-comparison-operators/" data-type="post" data-id="33245" target="_blank">compare</a> it against the currently shortest list stored in a separate variable. After the termination of the loop, the variable contains the shortest list.</p>
<p>Here’s a simple example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="1-6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_shortest_list(lst): shortest = lst[0] if lst else None for x in lst: if len(x) &lt; len(shortest): shortest = x return shortest print(get_shortest_list([[1], [2, 3], [4, 5, 6]]))
# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [[1], [2], [3]] print(get_shortest_list([]))
# None
</pre>
<p>So many lines of code! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f605.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> At least does the approach also work when passing in an empty list due to the ternary operator used in the first line.</p>
<p><code>lst[0] if lst else None</code></p>
<p>If you need a refresher on the ternary operator, you should check out our blog tutorial.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Training Tutorial</strong>: <a href="https://blog.finxter.com/python-one-line-ternary/" data-type="post" data-id="10641" target="_blank" rel="noreferrer noopener">The Ternary Operator — A Powerful Python Device</a></p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: If you need the <em>length </em>of the shortest list, you could simply replace the last line of the function with <code>return len(shortest)</code> , and you’re done!</p>
<h2>Summary</h2>
<p>You have learned about four ways to find the shortest list and its length from a Python list of lists (nested list):</p>
<ul>
<li><strong>Method 1</strong>: <code>min(lst, key=len)</code></li>
<li><strong>Method 2</strong>: <code>len(min(lst, key=len))</code></li>
<li><strong>Method 3</strong>: <code>min(len(x) for x in lst)</code></li>
<li><strong>Method 4</strong>: Naive For Loop</li>
</ul>
<p>I hope you found the tutorial helpful, if you did, feel free to consider joining our <a href="https://blog.finxter.com/subscribe/" data-type="page" data-id="1414" target="_blank" rel="noreferrer noopener">community of likeminded coders</a>—we do have lots of free training material!</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Also, check out our tutorial on <a href="https://blog.finxter.com/how-to-find-the-minimum-of-a-list-of-lists-in-python/" data-type="post" data-id="8461" target="_blank" rel="noreferrer noopener">How to Find the Minimum of a List of Lists in Python?</a>—it’s a slight variation!</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="768" height="432" src="https://blog.finxter.com/wp-content/uploads/2022/09/image-93.png" alt="" class="wp-image-713743" srcset="https://blog.finxter.com/wp-content/uploads/2022/09/image-93.png 768w, https://blog.finxter.com/wp-content/uploads/2022/09/image-93-300x169.png 300w" sizes="(max-width: 768px) 100vw, 768px" /></figure>
</div>
<p class="has-base-background-color has-background"> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Recommended Tutorial</strong>: <a href="https://blog.finxter.com/python-find-longest-list-in-dict-of-lists/" data-type="post" data-id="712778" target="_blank" rel="noreferrer noopener">Python Find Longest List in Dict of Lists</a></p>
</div>


https://www.sickgaming.net/blog/2022/09/24/python-find-shortest-list-in-list/