Downside Formulation
💬 Programming Problem: Given a record of lists (nested record). Discover and return the longest interior record from the outer record of lists.
Listed 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]
Additionally, you’ll discover ways to resolve a variant of this problem.
💬 Bonus problem: Discover solely the size of the longest record within the record of lists.
Listed 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 with out additional ado, let’s get began!
Technique 1: max(lst, key=len)
Use Python’s built-in max()
operate with a key argument to search out the longest record in a listing of lists. Name max(lst, key=len)
to return the longest record in lst
utilizing the built-in len()
operate to affiliate the load of every record, in order that the longest interior record would be the most.
Right here’s an instance:
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 good looking one-liner answer, isn’t it? 🙂 Let’s take a look at a slight variant to verify the size of the longest record as a substitute.
Technique 2: len(max(lst, key=len))
To get the size of the longest record in a nested record, use the len(max(lst, key=len))
operate. First, you identify the longest interior record utilizing the max()
operate with the important thing argument set to the len()
operate. Second, you go this longest record into the len()
operate itself to find out the utmost.
Right here’s an identical instance:
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
Technique 3: max(len(x) for x in lst)
A Pythonic technique to verify the size of the longest record is to mix a generator expression or record comprehension with the max()
operate with out key. As an illustration, max(len(x) for x in lst)
first turns all interior record into size integer numbers and passes this iterable into the max()
operate to get the outcome.
Right here’s this method on the identical examples as earlier than:
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
coaching impact might be obtained by learning the next tutorial on the subject—be happy to take action!
👉 Coaching: Understanding Checklist Comprehension in Python
Technique 4: Naive For Loop
A not so Pythonic however nonetheless high-quality method is to iterate over all lists in a for
loop, verify their size utilizing the len()
operate, and examine it towards the presently longest record saved in a separate variable. After the termination of the loop, the variable accommodates the longest record.
Right here’s a easy instance:
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 traces of code! 😅 A minimum of does the method additionally work when passing in an empty record as a result of ternary operator used within the first line.
lst[0] if lst else None
Should you want a refresher on the ternary operator, it’s best to try our weblog tutorial.
👉 Coaching Tutorial: The Ternary Operator — A Highly effective Python Machine
⭐ Observe: Should you want the size of the longest record, you could possibly merely exchange the final line of the operate with return len(longest)
, and also you’re achieved!
Abstract
You will have realized about 4 methods to search out the longest record and its size from a Python record of lists (nested record):
- Technique 1:
max(lst, key=len)
- Technique 2:
len(max(lst, key=len))
- Technique 3:
max(len(x) for x in lst)
- Technique 4: Naive For Loop
I hope you discovered the tutorial useful, if you happen to did, be happy to contemplate becoming a member of our group of likeminded coders—we do have numerous free coaching materials!
👉 Additionally, try our tutorial on discovering the final most of a listing of lists—it’s a slight variation!


Whereas working as a researcher in distributed techniques, Dr. Christian Mayer discovered his love for instructing laptop science college students.
To assist college students attain increased ranges of Python success, he based the programming training web site Finxter.com. He’s creator of the favored programming ebook Python One-Liners (NoStarch 2020), coauthor of the Espresso Break Python sequence of self-published books, laptop science fanatic, freelancer, and proprietor of one of many prime 10 largest Python blogs worldwide.
His passions are writing, studying, and coding. However his best ardour is to serve aspiring coders via Finxter and assist them to spice up their abilities. You possibly can be part of his free e-mail academy right here.