• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Welcome You have to register before you can post on our site. Username: Password: Remember me

 Discord Widget

 Latest Threads [Oracle Blog] Java on Container Like A P... Last Post: xSicKxBot Yesterday 11:30 PM » Replies: 0 » Views: 1 [Tut] Python | Split String After Delimi... Last Post: xSicKxBot Yesterday 11:30 PM » Replies: 0 » Views: 1 (Indie Deal) Monster Spark Bundle & Blac... Last Post: xSicKxBot Yesterday 11:29 PM » Replies: 0 » Views: 1 PC - Dying Light 2 Stay Human: Bloody Ti... Last Post: xSicKxBot Yesterday 11:29 PM » Replies: 0 » Views: 1 News - Apex Legends Mobile, Inscryption ... Last Post: xSicKxBot Yesterday 11:29 PM » Replies: 0 » Views: 1 [Oracle Blog] Java and the New Duke Pers... Last Post: xSicKxBot Yesterday 04:54 AM » Replies: 0 » Views: 11 [Tut] 3 Easy Habits That Can Make You Ri... Last Post: xSicKxBot Yesterday 04:54 AM » Replies: 0 » Views: 4 (Indie Deal) Vorax Development update #1 Last Post: xSicKxBot Yesterday 04:53 AM » Replies: 0 » Views: 3 PC - Among Us VR Last Post: xSicKxBot Yesterday 04:53 AM » Replies: 0 » Views: 3 News - Best Team In NFL Plays GTA Online... Last Post: xSicKxBot Yesterday 04:53 AM » Replies: 0 » Views: 3

[Tut] Python Find Shortest List in List

Python Find Shortest List in List

5/5 – (1 vote)

## Problem Formulation

Programming Challenge: Given a list of lists (nested list). Find and return the shortest inner list from the outer list of lists.

Here are some examples:

• `[[1], [2, 3], [4, 5, 6]]` `[1]`
• `[[1, [2, 3], 4], [5, 6], [7]]` `[7]`
• `[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]` `[[1], [2], [3]]`

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

Bonus challenge: Find only the length of the shortest list in the list of lists.

Here are some examples:

• `[[1], [2, 3], [4, 5, 6]]` `1`
• `[[1, [2, 3], 4], [5, 6], [7]]` `1`
• `[[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]` `3`

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 in a list of lists. Call `min(lst, key=len)` to return the shortest list in `lst` using the built-in `len()` function to associate the weight of each list, so that the shortest inner list will be the minimum.

Here’s an example:

```def get_shortest_list(lst): return min(lst, key=len) print(get_shortest_list([[1], [2, 3], [4, 5, 6]]))
# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [[1], [2], [3]]
```

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

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

To get the length of the shortest list in a nested list, use the `len(min(lst, key=len))` function. First, you determine the shortest inner list using the `min()` function with the key argument set to the `len()` function. Second, you pass this shortest list into the `len()` function itself to determine the minimum.

Here’s an analogous example:

```def get_length_of_shortest_list(lst): return len(min(lst, key=len)) print(get_length_of_shortest_list([[1], [2, 3], [4, 5, 6]]))
# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 3
```

## Method 3: min(len(x) for x in lst)

A Pythonic way to check the length of the shortest list is to combine a generator expression or list comprehension with the `min()` function without key. For instance, `min(len(x) for x in lst)` first turns all inner list into length integer numbers and passes this iterable into the `min()` function to get the result.

Here’s this approach on the same examples as before:

```def get_length_of_shortest_list(lst): return min(len(x) for x in lst) print(get_length_of_shortest_list([[1], [2, 3], [4, 5, 6]]))
# 1 print(get_length_of_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# 1 print(get_length_of_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# 3
```

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

## Method 4: Naive For Loop

A not so Pythonic but still fine approach is to iterate over all lists 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:

```def get_shortest_list(lst): shortest = lst[0] if lst else None for x in lst: if len(x) < len(shortest): shortest = x return shortest print(get_shortest_list([[1], [2, 3], [4, 5, 6]]))
# [1] print(get_shortest_list([[1, [2, 3], 4], [5, 6], [7]]))
# [7] print(get_shortest_list([[[1], [2], [3]], [4, 5, [6]], [7, 8, 9, 10]]))
# [[1], [2], [3]] print(get_shortest_list([]))
# None
```

So many lines of code! At least does the approach also work when passing in an empty list due to the ternary operator used in the first line.

`lst[0] if lst else None`

If you need a refresher on the ternary operator, you should check out our blog tutorial.

Training Tutorial: The Ternary Operator — A Powerful Python Device

Note: If you need the length of the shortest list, you could simply replace the last line of the function with `return len(shortest)` , and you’re done!

## Summary

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

• Method 1: `min(lst, key=len)`
• Method 2: `len(min(lst, key=len))`
• Method 3: `min(len(x) for x in lst)`
• Method 4: Naive For Loop

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!

Also, check out our tutorial on How to Find the Minimum of a List of Lists in Python?—it’s a slight variation!

Recommended Tutorial: Python Find Longest List in Dict of Lists

https://www.sickgaming.net/blog/2022/09/...t-in-list/

 Users browsing this thread: 1 Guest(s)

πΜ·πΜ·πΜ·πΜ·πΆΜ·πΜ·πΜ·πΜ·πΜ·πΜ· π·Μ·πΜ·πΜ·πΜ·