How to Find the Minimum of a List of Lists in Python?

Short answer: To find the minimal list in a list of lists, you need to make two lists comparable. How? With the key argument of the min() function. The key argument is a function that takes one input (a list) and returns one output (a numerical value). The list with the smallest numerical value is returned as the minimum of the list of lists.

Problem: Say you have a list of lists (nested list) and you want to find the minimum of this list. It’s not trivial to compare lists—what’s the minimum among lists after all? To define the minimum among the inner lists, you may want to consider different objectives.

  1. The first element of each inner list.
  2. The i-th element of each inner list.
  3. The sum of inner list elements.
  4. The maximum of inner list elements.
  5. The minimum of inner list elements.

Example: Given list of lists [[1, 1, 1], [0, 2, 0], [3, 3, -1]]. Which is the minimum element?

  1. The first element of each inner list. The minimum is [0, 2, 0].
  2. The i-th element of each inner list (i = 2). The minimum is [3, 3, -1].
  3. The sum of inner list elements. The minimum is [0, 2, 0].
  4. The maximum of inner list elements. The minimum is [1, 1, 1].
  5. The minimum of inner list elements. The minimum is [3, 3, -1].

So how do you accomplish this?

Solution: Use the min() function with key argument.

Syntax: The min() function is a built-in function in Python (Python versions 2.x and 3.x). Here’s the syntax:

min(iterable, key=None)

Arguments:

ArgumentDescription
iterableThe values among which you want to find the minimum. In our case, it’s a list of lists.
key(Optional. Default None.) Pass a function that takes a single argument and returns a comparable value. The function is then applied to each element in the list. Then, the method find the minimum based on the key function results rather than the elements themselves.

Let’s study the solution code for our different versions of calculating the minimal “list” of a list of lists (nested list).

Try it yourself in our interactive code shell:

lst = [[1, 1, 1], [0, 2, 0], [3, 3, -1]]

# Minimum using first element
print(min(lst, key=lambda x: x[0]))
# [0, 2, 0]

# Minimum using third element
print(min(lst, key=lambda x: x[2]))
# [3, 3, -1]

# Minimum using sum()
print(min(lst, key=sum))
# [0, 2, 0]

# Minimum using max
print(min(lst, key=max))
# [1, 1, 1]

# Minimum using min
print(min(lst, key=min))
# [3, 3, -1]

Related Video:

Related articles:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!