Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Find Longest List in List

#1
Python Find Longest List in List

5/5 – (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]], [4, 5, [6]], [7, 8, 9, 10]] ? [7, 8, 9, 10]

Also, you’ll learn how to solve a variant of this challenge.

? Bonus challenge: Find only the length of the longest list in the list of lists.

Here are some examples:

  • [[1], [2, 3], [4, 5, 6]] ? 3
  • [[1, [2, 3], 4], [5, 6], [7]] ? 3
  • [[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]] ? 4

So without further ado, let’s get started!

Method 1: max(lst, key=len)


Use Python’s built-in max() function with a key argument to find the longest list in a list of lists. Call max(lst, key=len) to return the longest list in lst using the built-in len() function to associate the weight of each list, so that the longest inner list will be the maximum.

Here’s an example:

def get_longest_list(lst): return max(lst, key=len) print(get_longest_list([[1], [2, 3], [4, 5, 6]]))
# [4, 5, 6] print(get_longest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [1, [2, 3], 4] print(get_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [7, 8, 9, 10]

A beautiful one-liner solution, isn’t it? ? Let’s have a look at a slight variant to check the length of the longest list instead.

Method 2: len(max(lst, key=len))


To get the length of the longest list in a nested list, use the len(max(lst, key=len)) function. First, you determine the longest inner list using the max() function with the key argument set to the len() function. Second, you pass this longest list into the len() function itself to determine the maximum.

Here’s an analogous example:

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]]))
# 3 print(get_length_of_longest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 3 print(get_length_of_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 4

Method 3: max(len(x) for x in lst)


A Pythonic way to check the length of the longest list is to combine a generator expression or list comprehension with the max() function without key. For instance, max(len(x) for x in lst) first turns all inner list into length integer numbers and passes this iterable into the max() function to get the result.

Here’s this approach on the same examples as before:

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]]))
# 3 print(get_length_of_longest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 3 print(get_length_of_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 4

A good training effect can be obtained by studying the following tutorial on the topic—feel free to do so!

? Training: Understanding List Comprehension in Python

Method 4: Naive For Loop


A not so Pythonic but still fine approach is to iterate over all lists in a for loop, check their length using the len() function, and compare it against the currently longest list stored in a separate variable. After the termination of the loop, the variable contains the longest list.

Here’s a simple example:

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]]))
# [4, 5, 6] print(get_longest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [1, [2, 3], 4] print(get_longest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [7, 8, 9, 10] print(get_longest_list([]))
# None

So many lines of code! ? At least does the approach also work when passing in an empty list due to the ternary operator used in the first line.

lst[0] if lst else None

If you need a refresher on the ternary operator, you should check out our blog tutorial.

? Training Tutorial: The Ternary Operator — A Powerful Python Device

⭐ Note: If you need the length of the longest list, you could simply replace the last line of the function with return len(longest) , and you’re done!

Summary


You have learned about four ways to find the longest list and its length from a Python list of lists (nested list):

  • Method 1: max(lst, key=len)
  • Method 2: len(max(lst, key=len))
  • Method 3: max(len(x) for x in lst)
  • Method 4: Naive For Loop

I hope you found the tutorial helpful, if you did, feel free to consider joining our community of likeminded coders—we do have lots of free training material!

? Also, check out our tutorial on finding the general maximum of a list of lists—it’s a slight variation!


YouTube Video



https://www.sickgaming.net/blog/2022/09/...t-in-list/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,050 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 2,104 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,955 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,555 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,695 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,606 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,511 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,530 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String into List of Substrings xSicKxBot 0 1,439 12-11-2022, 12:17 PM
Last Post: xSicKxBot
  [Tut] Python Find in List [Ultimate Guide] xSicKxBot 0 1,428 12-09-2022, 11:35 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016