w3resource

Python: Find the list with maximum and minimum length

Python List: Exercise - 91 with Solution

Write a Python program to find a list with maximum and minimum lengths.

Sample Solution:

Python Code:

def max_length_list(input_list):
    max_length = max(len(x) for x in input_list )
    max_list = max(input_list, key = len)
    return(max_length, max_list)
def min_length_list(input_list):
    min_length = min(len(x) for x in input_list )
    min_list = min(input_list, key = len)
    return(min_length, min_list)
list1 = [[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]
print("Original list:")
print(list1)
print("\nList with maximum length of lists:")
print(max_length_list(list1))
print("\nList with minimum length of lists:")
print(min_length_list(list1))
list1 =  [[0], [1, 3], [5, 7], [9, 11], [3, 5, 7]]
print("Original list:")
print(list1)
print("\nList with maximum length of lists:")
print(max_length_list(list1))
print("\nList with minimum length of lists:")
print(min_length_list(list1))
list1 =  [[12], [1, 3], [1, 34, 5, 7], [9, 11], [3, 5, 7]]
print("Original list:")
print(list1)
print("\nList with maximum length of lists:")
print(max_length_list(list1))
print("\nList with minimum length of lists:")
print(min_length_list(list1))

Sample Output:

Original list:
[[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]]

List with maximum length of lists:
(3, [13, 15, 17])

List with minimum length of lists:
(1, [0])
Original list:
[[0], [1, 3], [5, 7], [9, 11], [3, 5, 7]]

List with maximum length of lists:
(3, [3, 5, 7])

List with minimum length of lists:
(1, [0])
Original list:
[[12], [1, 3], [1, 34, 5, 7], [9, 11], [3, 5, 7]]

List with maximum length of lists:
(4, [1, 34, 5, 7])

List with minimum length of lists:
(1, [12])

Pictorial Presentation:

Python List: Find the list with maximum and minimum length.

Flowchart:

Flowchart: Find the list with maximum and minimum length.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to count number of lists in a given list of lists.
Next: Write a Python program to check if a nested list is a subset of another nested list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Decapitalizes the first letter of a string:

Example:

def tips_decapitalize(s, upper_rest=False):
  return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:])
print(tips_decapitalize('PythonTips'))
print(tips_decapitalize('PythonTips', True)) 

Output:

pythonTips
pYTHONTIPS

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook