5 Best Ways to Find the Sum of the Minimums of Each Sublist in Python

πŸ’‘ Problem Formulation: You are tasked with writing a Python program that takes a list of sublists and returns the sum of the minimum element from each sublist. For example, given the input [[7, 5, 3], [2, 4], [5], [3, 8]], the desired output is 3 + 2 + 5 + 3 = 13 as these are the minimums of the individual sublists.

Method 1: Using a Simple for Loop

This traditional approach involves iterating over each sublist and finding its minimum using a nested for loop. The minimum values are then accumulated to find the total sum. This method is straightforward and easily understandable.

Here’s an example:

def sum_of_mins(list_of_lists):
    total_sum = 0
    for sublist in list_of_lists:
        min_value = min(sublist)
        total_sum += min_value
    return total_sum

# Example usage:
print(sum_of_mins([[7, 5, 3], [2, 4], [5], [3, 8]]))

Output:

13

This code defines a function sum_of_mins() that loops through each sublist, utilizes the built-in min() function to find the smallest element, and adds it to the running total. As expected, the output of the example usage is 13, confirming its correctness.

Method 2: List Comprehension with sum() and min()

List comprehension in Python provides a concise way to create lists. By using the sum() function in combination with min() inside a list comprehension, one can calculate the required sum in a single line of code.

Here’s an example:

list_of_lists = [[7, 5, 3], [2, 4], [5], [3, 8]]
print(sum([min(sublist) for sublist in list_of_lists]))

Output:

13

The example uses list comprehension to iterate over all sublists, finding the minimum of each and creating a new list. The sum() function then calculates the total sum of all the minimum values, resulting in the expected output 13.

Method 3: Using map() and sum()

The map() function applies a given function to each item of an iterable and returns a list of the results. Pairing map() with min() can elegantly determine the sum of minimums.

Here’s an example:

list_of_lists = [[7, 5, 3], [2, 4], [5], [3, 8]]
print(sum(map(min, list_of_lists)))

Output:

13

The map(min, list_of_lists) part of this code snippet applies the min() function to each sublist in list_of_lists, which returns an iterable of the minimum values. The sum() function then adds these values up, providing a neat and pythonic solution.

Method 4: Functional Approach with functools.reduce()

Python’s functools module offers higher-order functions and operations on callable objects, such as reduce(). This method applies a cumulative function to successive pairs of values in a list.

Here’s an example:

from functools import reduce

list_of_lists = [[7, 5, 3], [2, 4], [5], [3, 8]]
print(reduce(lambda acc, x: acc + min(x), list_of_lists, 0))

Output:

13

This code utilizes reduce() from the functools module to accumulate the total sum. The lambda function within reduce() manages the summation process by adding the minimum of each sublist (found by min(x)) to the accumulator acc.

Bonus One-Liner Method 5: Using Generator Expression

A generator expression is similar to a list comprehension, but it generates items one by one, which is more memory-efficient. It can be used directly within the sum() function to find the desired sum.

Here’s an example:

list_of_lists = [[7, 5, 3], [2, 4], [5], [3, 8]]
print(sum(min(sublist) for sublist in list_of_lists))

Output:

13

This compact solution uses a generator expression to create an iterator that yields the minimum of each sublist. The sum() function then iterates through these values to calculate the total sum dynamically.

Summary/Discussion

  • Method 1: Simple for Loop. Easy for beginners to understand. Less efficient for larger lists due to explicit looping.
  • Method 2: List Comprehension. More Pythonic and compact. Can potentially use more memory if the list is extensive.
  • Method 3: Using map(). Clean and functional style. Suitable for single-line usage.
  • Method 4: Functional Approach with functools.reduce(). Leverages higher-order functions. Slightly less readable for those unfamiliar with functional programming concepts.
  • Method 5: Generator Expression. Most memory-efficient. Ideal for large datasets but may be confusing to beginners.