{"id":128335,"date":"2022-09-24T19:25:12","date_gmt":"2022-09-24T19:25:12","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=713706"},"modified":"2022-09-24T19:25:12","modified_gmt":"2022-09-24T19:25:12","slug":"python-find-shortest-list-in-list","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/24\/python-find-shortest-list-in-list\/","title":{"rendered":"Python Find Shortest List in List"},"content":{"rendered":"\n<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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<h2>Problem Formulation<\/h2>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<p>Here are some examples:<\/p>\n<ul>\n<li><code>[[1], [2, 3], [4, 5, 6]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>[1]<\/code><\/li>\n<li><code>[[1, [2, 3], 4], [5, 6], [7]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>[7]<\/code><\/li>\n<li><code>[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>[[1], [2], [3]]<\/code><\/li>\n<\/ul>\n<p>Also, you&#8217;ll learn how to solve a variant of this challenge.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" 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>\n<p>Here are some examples:<\/p>\n<ul>\n<li><code>[[1], [2, 3], [4, 5, 6]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>1<\/code><\/li>\n<li><code>[[1, [2, 3], 4], [5, 6], [7]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>1<\/code><\/li>\n<li><code>[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]<\/code> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <code>3<\/code><\/li>\n<\/ul>\n<p>So without further ado, let&#8217;s get started!<\/p>\n<h2>Method 1: min(lst, key=len)<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">Use Python\u2019s 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>\n<p>Here&#8217;s an example:<\/p>\n<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]]))\n# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# [[1], [2], [3]]\n<\/pre>\n<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&#8217;t it? <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> Let&#8217;s have a look at a slight variant to check the <em>length <\/em>of the shortest list instead.<\/p>\n<h2>Method 2: len(min(lst, key=len))<\/h2>\n<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>\n<p>Here&#8217;s an analogous example:<\/p>\n<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]]))\n# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# 3\n<\/pre>\n<h2>Method 3: min(len(x) for x in lst)<\/h2>\n<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>\n<p>Here&#8217;s this approach on the same examples as before:<\/p>\n<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]]))\n# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# 3\n<\/pre>\n<p>A good training effect can be obtained by studying the following tutorial on the topic&#8212;feel free to do so!<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<h2>Method 4: Naive For Loop<\/h2>\n<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>\n<p>Here&#8217;s a simple example:<\/p>\n<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]]))\n# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# [[1], [2], [3]] print(get_shortest_list([]))\n# None\n<\/pre>\n<p>So many lines of code! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f605.png\" alt=\"\ud83d\ude05\" 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>\n<p><code>lst[0] if lst else None<\/code><\/p>\n<p>If you need a refresher on the ternary operator, you should check out our blog tutorial.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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 &#8212; A Powerful Python Device<\/a><\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" 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&#8217;re done!<\/p>\n<h2>Summary<\/h2>\n<p>You have learned about four ways to find the shortest list and its length from a Python list of lists (nested list):<\/p>\n<ul>\n<li><strong>Method 1<\/strong>: <code>min(lst, key=len)<\/code><\/li>\n<li><strong>Method 2<\/strong>: <code>len(min(lst, key=len))<\/code><\/li>\n<li><strong>Method 3<\/strong>: <code>min(len(x) for x in lst)<\/code><\/li>\n<li><strong>Method 4<\/strong>: Naive For Loop<\/li>\n<\/ul>\n<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>&#8212;we do have lots of free training material!<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>&#8212;it&#8217;s a slight variation!<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<\/div>\n<p class=\"has-base-background-color has-background\"> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Problem Formulation Programming Challenge: Given a list of lists (nested list). Find and return the shortest inner list from the outer list of lists. Here are some examples: [[1], [2, 3], [4, 5, 6]] [1] [[1, [2, 3], 4], [5, 6], [7]] [7] [[[1], [2], [3]], [4, 5, [6]], [7, 8, [&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-128335","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\/128335","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=128335"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128335\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}