# Python Find Shortest List in Dict of Lists

5/5 - (1 vote)

## Problem Formulation

π¬ Programming Challenge: Given a dictionary where the values are lists of varying sizes. Find and return the shortest list!

Here’s an example:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}
# Goal: [1]```

Also, you’ll learn how to solve a variant of this challenge.

π¬ Bonus challenge: Find only the key that is associated with the shortest list in the dictionary.

Here’s an example:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}
# Goal: 1```

So without further ado, let’s get started!

## Method 1: min(lst, key=len)

Use Pythonβs built-in `min()` function with a key argument to find the shortest list from the dictionary values. Call `min(d.values(), key=len)` to return the shortest list in `d.values()` using the built-in `len()` function to associate the weight of each list, so that the shortest list will be the minimum.

Here’s an example:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}

print(min(d.values(), key=len))
# [1]```

A beautiful one-liner solution, isn’t it? π Let’s have a look at a slight variant to check the key of the shortest list instead.

π Recommended Tutorial: Python Find Longest List in Dict

## Method 2: len(min(lst, key=len))

To get the key mapping to the shortest list value in a dictionary, use `min(d, key=lambda x: len(d[x]))`.

Explanation: You set the key argument of `min()` to a lambda function that maps each dictionary key `x` to the length of the associated value obtained with `len(d[x])`. This way, you use the length of the mapped values as weights to determine the “minimum”, i.e., the shortest list.

π‘ Note: When you call `min(d)` Python will automatically replace it with `min(d.keys())`, so it iterates over the keys of the dictionary.

Here’s an analogous example:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}

print(min(d, key=lambda x: len(d[x])))
# 1
```

The key with shortest list in the dictionary is `4`.

π Recommended Tutorial: A Complete Guide to Python Dictionaries

## Method 4: Shortest List in Dict Values Using List Comprehension

You can also get the length of the shortest list from the dictionary values by combining a generator expression or list comprehension with the `min()` function without key using the following expression: `min(len(val) for val in d.values())`

Like so:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}

print(min(len(val) for val in d.values()))
# 1
```

Note that this returns the length of the shortest list in the dict values, not the key or the list itself.

A good training effect can be obtained by studying the following tutorial on the topic—feel free to do so!

π Training: Understanding List Comprehension in Python

## Method 4: Naive For Loop

A not so Pythonic but still fine approach is to iterate over all key-value pairs in a `for` loop, check their length using the `len()` function, and compare it against the currently shortest list stored in a separate variable. After the termination of the loop, the variable contains the shortest list.

Here’s a simple example:

```d = {1: [1, 2, 3],
2: [1, 2],
3: [1],
4: [1, 2, 3, 4]}

# Initialize variables with dummy values
k_min, v_min = -1, []
i = 0

for key,val in d.items():
if len(val) < len(v_min) or i==0:
k_min, v_min = key, val
i += i

print("Shortest key and value:", str(k_min) + ',' + str(v_min))
# Shortest key and value: 3,[1]```

So many lines of code! π I hate it.

## Summary

You have learned about four ways to find the shortest list and its length from a Python list of lists (nested list):

I hope you found the tutorial helpful. If you did, feel free to consider joining our community of likeminded coders—we do have lots of free training material!

π Recommended Tutorial: Python Find Longest List in List