{"id":128274,"date":"2022-09-22T19:35:26","date_gmt":"2022-09-22T19:35:26","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=707208"},"modified":"2022-09-22T19:35:26","modified_gmt":"2022-09-22T19:35:26","slug":"python-find-longest-list-in-list","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/22\/python-find-longest-list-in-list\/","title":{"rendered":"Python Find Longest 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;707208&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 longest 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>[4, 5, 6]<\/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, [2, 3], 4]<\/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>[7, 8, 9, 10]<\/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 longest 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>3<\/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>3<\/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>4<\/code><\/li>\n<\/ul>\n<p>So without further ado, let&#8217;s get started!<\/p>\n<h2>Method 1: max(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-max\/\" target=\"_blank\">max()<\/a><\/code> function with a key argument to find the longest list in a list of lists. Call <code>max(lst, key=len)<\/code> to return the longest 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 longest inner list will be the maximum.<\/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_longest_list(lst): return max(lst, key=len) print(get_longest_list([[1], [2, 3], [4, 5, 6]]))\n# [4, 5, 6] print(get_longest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# [1, [2, 3], 4] print(get_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# [7, 8, 9, 10]<\/pre>\n<p>A beautiful <a href=\"https:\/\/pythononeliners.com\/\" data-type=\"URL\" data-id=\"https:\/\/pythononeliners.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">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 longest list instead.<\/p>\n<h2>Method 2: len(max(lst, key=len))<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">To get the length of the longest list in a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\">n<\/a><a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\" rel=\"noreferrer noopener\">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(max(lst, key=len))<\/code> function. First, you determine the longest inner list using the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-max\/\" data-type=\"post\" data-id=\"21661\" target=\"_blank\">max()<\/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 longest list into the <code>len()<\/code> function itself to determine the maximum.<\/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_longest_list(lst): return len(max(lst, key=len)) print(get_length_of_longest_list([[1], [2, 3], [4, 5, 6]]))\n# 3 print(get_length_of_longest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# 3 print(get_length_of_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# 4\n<\/pre>\n<h2>Method 3: max(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 longest list is to combine a <a href=\"https:\/\/blog.finxter.com\/how-to-use-generator-expressions-in-python-dictionaries\/\" data-type=\"post\" data-id=\"7502\" target=\"_blank\" rel=\"noreferrer noopener\">generator expression<\/a> or <a href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\">list comprehension<\/a> with the <code>max()<\/code> function without key. For instance, <code>max(len(x) for x in lst)<\/code> first turns all inner list into length integer numbers and passes this iterable into the <code>max()<\/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_longest_list(lst): return max(len(x) for x in lst) print(get_length_of_longest_list([[1], [2, 3], [4, 5, 6]]))\n# 3 print(get_length_of_longest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# 3 print(get_length_of_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# 4\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 longest list stored in a separate variable. After the termination of the loop, the variable contains the longest 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_longest_list(lst): longest = lst[0] if lst else None for x in lst: if len(x) > len(longest): longest = x return longest print(get_longest_list([[1], [2, 3], [4, 5, 6]]))\n# [4, 5, 6] print(get_longest_list([[1, [2, 3], 4], [5, 6], [7]]))\n# [1, [2, 3], 4] print(get_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))\n# [7, 8, 9, 10] print(get_longest_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 length of the longest list, you could simply replace the last line of the function with <code>return len(longest)<\/code> , and you&#8217;re done!<\/p>\n<h2>Summary<\/h2>\n<p>You have learned about four ways to find the longest list and its length from a Python list of lists (nested list):<\/p>\n<ul>\n<li><strong>Method 1<\/strong>: <code>max(lst, key=len)<\/code><\/li>\n<li><strong>Method 2<\/strong>: <code>len(max(lst, key=len))<\/code><\/li>\n<li><strong>Method 3<\/strong>: <code>max(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 rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-find-the-max-of-list-of-lists-in-python\/\" data-type=\"post\" data-id=\"8422\" target=\"_blank\">finding the general maximum of a list of lists<\/a>&#8212;it&#8217;s a slight variation!<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"432\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-89.png\" alt=\"\" class=\"wp-image-707377\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-89.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/09\/image-89-300x169.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-find-longest-list-in-list\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FY1ZKmCfqCZk%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\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 longest inner list from the outer list of lists. Here are some examples: [[1], [2, 3], [4, 5, 6]] [4, 5, 6] [[1, [2, 3], 4], [5, 6], [7]] [1, [2, 3], 4] [[[1], [2], [3]], [&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-128274","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\/128274","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=128274"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128274\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}