Python - Find maximum length sub-list in a nested list Last Updated : 27 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Python, we often work with nested lists (lists inside lists), and sometimes we need to find out which sub-list has the most items. In this article, we will explore Various methods to Find the maximum length of a sub-list in a nested list. Using max() Function with key=lenThe simplest and most efficient way to find the longest sub-list is to use the built-in max() function. The max() function helps us find the largest item in a list. By using the key=len argument, we tell Python to compare the lengths of the sub-lists instead of the sub-lists themselves. Python a = [[1, 2], [3, 4, 5], [6]] # Find the longest sub-list b = max(a, key=len) print(b) Output[3, 4, 5] Other methods of finding the maximum length sub-list in a nested list are:Table of ContentUsing a Loop Using List Comprehension with zip()Using sorted() with reverse=TrueUsing a Loop Iterate through the nested list and keep track of the sub-list with the maximum length. This method gives us the flexibility to add more logic or custom conditions if needed. Python a = [[1, 2], [3, 4, 5], [6]] # Initialize variables to track the longest sub-list b = [] max_length = 0 # Loop through each sub-list in the nested list for sub_list in a: if len(sub_list) > max_length: b = sub_list max_length = len(sub_list) print(b) Output[3, 4, 5] Using List Comprehension with zip()List comprehension is a Python feature that lets us write compact and readable code. Generate a tuple of lengths and corresponding sub-lists, then extract the sub-list with the maximum length. Python a = [[1, 2], [3, 4, 5], [6]] # Use list comprehension to find sub-lists with the maximum length b = max((sublist for sublist in a), key=len) print(b) Output[3, 4, 5] Using sorted() with reverse=TrueAnother method to find the longest sub-list is by sorting the nested list by the length of the sub-lists in descending order. After sorting, the first element will be the longest sub-list. Python a = [[1, 2], [3, 4, 5], [6]] # Sort the list by length of sub-lists in descending order sorted_lists = sorted(a, key=len, reverse=True) # first element is the longest sub-list b = sorted_lists[0] print(b) Output[3, 4, 5] Comment More infoAdvertise with us Next Article Python - Find maximum length sub-list in a nested list S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python | Find the sublist with maximum value in given nested list Given a list of list, the task is to find sublist with the maximum value in second column. Examples: Input : [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]] Output :['Labs', 532] Input: [['Geek', 90], ['For', 32], ['Geeks', 120]] Output: ['Geeks', 120] Below are some tasks 4 min read Python - Maximum column values in mixed length 2D List The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the maximizations of its columns, the uneven length of rows may lead to some elements in that elements to be absent and if not handled correctly, may throw an exce 6 min read Python - Find starting index of all Nested Lists In this article given a matrix, the task is to write a Python program to compute the starting index of all the nested lists. Example: Input : test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]] Output : [0, 1, 5, 7, 13] Explanation : 1 + 4 = lengths of 2 initial lists = 5, 3, of 3 8 min read Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e 3 min read Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U 6 min read Like