Python | Find maximum length sub-list in a nested list
Given a list of lists, write a Python program to find the list with maximum length. The output should be in the form (list, list_length).
Examples:
Input : [['A'], ['A', 'B'], ['A', 'B', 'C']] Output : (['A', 'B', 'C'], 3) Input : [[1, 2, 3, 9, 4], [5], [3, 8], [2]] Output : ([1, 2, 3, 9, 4], 5)
Let’s discuss different approaches to solve this problem.
Approach #1 : Using for loop (Naive)
This is a brute force method in which we iterate through each list item(list) and find the list with maximum length. Similarly, we use the for loop to find length of each list and output the maximum length.
# Python3 program to Find maximum # length list in a nested list def FindMaxLength(lst): maxList = max((x) for x in lst) maxLength = max(len(x) for x in lst ) return maxList, maxLength # Driver Code lst = [['A'], ['A', 'B'], ['A', 'B', 'C']] print(FindMaxLength(lst)) |
(['A', 'B', 'C'], 3)
Approach #2 : Using map
In this method we use Python map function to iterate over the inner lists to create a list of lengths, then get the maximum with max function.
# Python3 program to Find maximum # length list in a nested list def FindMaxLength(lst): maxList = max(lst, key = len) maxLength = max(map(len, lst)) return maxList, maxLength # Driver Code lst = [['A'], ['A', 'B'], ['A', 'B', 'C'], ] print(FindMaxLength(lst)) |
(['A', 'B', 'C'], 3)
Approach #3 : Using lambda operator
One more method in Python to find the longest length list is the lambda operator. It is used for creating small, one-time and anonymous function objects in Python. Here we pass a variable i as argument in the len(i) expression and find the maximum length.
# Python3 program to Find maximum # length list in a nested list def FindMaxLength(lst): maxList = max(lst, key = lambda i: len(i)) maxLength = len(maxList) return maxList, maxLength # Driver Code lst = [['A'], ['A', 'B'], ['A', 'B', 'C']] print(FindMaxLength(lst)) |
(['A', 'B', 'C'], 3)
Recommended Posts:
- Python | Find the sublist with maximum value in given nested list
- Python | Find maximum value in each sublist
- Python | Convert nested sublist into tuples
- Python | Check if a nested list is a subset of another nested list
- Python | Maximum Sum Sublist
- Python | Replace sublist with other in list
- Python | Shift sublist in list
- Python | Sort a list according to the second element in sublist
- Python | Ways to find length of list
- Python | Pair and combine nested list to tuple list
- Python | Convert string List to Nested Character List
- Python | Convert a nested list into a flat list
- Python | Convert given list into nested list
- Python | Test for nested list
- Python | Intersection of two nested list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

