![]() |
|
[Tut] Python Find Longest List in List - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Find Longest List in List (/thread-99978.html) |
[Tut] Python Find Longest List in List - xSicKxBot - 09-23-2022 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:
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:
So without further ado, let’s get started! Method 1: max(lst, key=len) Use Python’s built-in Code: max()Code: max(lst, key=len)Code: lstCode: len()Here’s an example: Code: 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 Code: len(max(lst, key=len))Code: max()Code: len()Code: len()Here’s an analogous example: Code: 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]]))
# 4A Pythonic way to check the length of the longest list is to combine a generator expression or list comprehension with the Code: max()Code: max(len(x) for x in lst)Code: max()Here’s this approach on the same examples as before: Code: 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]]))
# 4A good training effect can be obtained by studying the following tutorial on the topic—feel free to do so! Training: Understanding List Comprehension in PythonMethod 4: Naive For Loop A not so Pythonic but still fine approach is to iterate over all lists in a Code: forCode: len()Here’s a simple example: Code: 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([]))
# NoneSo 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.Code: lst[0] if lst else NoneIf 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 Code: return len(longest)Summary You have learned about four ways to find the longest list and its length from a Python list of lists (nested list):
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!![]() |