# Python – Length of shortest string in string list

Last Updated : 05 May, 2023

Sometimes, while working with a lot of data, we can have a problem in which we need to extract the minimum length string of all the strings in list. This kind of problem can have applications in many domains. Letâ€™s discuss certain ways in which this task can be performed.

Method #1 : Using min() + generator expression

The combination of the above functionalities can be used to perform this task. In this, we extract all the list lengths using the generator expression and return a minimum of them using min().

## Python3

 `# Python3 code to demonstrate working of` `# Minimum String length` `# using min() + generator expression`   `# initialize list` `test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``]`   `# printing original list` `print``(``"The original list : "` `+` `str``(test_list))`   `# Minimum String length` `# using min() + generator expression` `res ``=` `min``(``len``(ele) ``for` `ele ``in` `test_list)`   `# printing result` `print``(``"Length of minimum string is : "` `+` `str``(res))`

Output :

```The original list : ['gfg', 'is', 'best']
Length of minimum string is : 2```

Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the list.

Method #2 : Using len() + key argument + min()

The combination of the above functions can be used to perform this task. In this, we extract the minimum length using len() and min(). It is faster than the above method as it performs more tasks built-in rather than overhead by generator expression.

## Python3

 `# Python3 code to demonstrate working of` `# Minimum String length` `# using len() + key argument + min()`   `# Initialize list` `test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``]`   `# Printing original list` `print``(``"The original list : "` `+` `str``(test_list))`   `# Minimum String length` `# using len() + key argument + min()` `res ``=` `len``(``min``(test_list, key``=``len``))`   `# Printing result` `print``(``"Length of minimum string is : "` `+` `str``(res))`

Output :

```The original list : ['gfg', 'is', 'best']
Length of minimum string is : 2```

Time Complexity: O(N), where n is the length of the input list. This is because weâ€™re using len() + key argument + min() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as weâ€™re using constant additional space.

Method #3 : Using reduce() + lambda function

The reduce function can be used to iterate over the list and use the lambda function to find the minimum length of the string.

In this method, we are using the reduce function from the functools module to find the minimum string length in the list. The reduce function applies the lambda function to the first two elements of the list, then to the result and the next element, and so on. In this case, the lambda function compares the length of x and y and returns the one which has the minimum length. Finally, we are getting the length of the returned string and printing it.

## Python3

 `# Python3 code to demonstrate working of` `# Minimum String length` `# using reduce() + lambda function`   `from` `functools ``import` `reduce`   `# Initialize list` `test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``]`   `# Printing original list` `print``(``"The original list : "` `+` `str``(test_list))`   `# Minimum String length` `# using reduce() + lambda function` `res ``=` `reduce``(``lambda` `x, y: x ``if` `len``(x) < ``len``(y) ``else` `y, test_list)`   `print``(``"Length of minimum string is : "` `+` `str``(``len``(res)))`   `# This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list : ['gfg', 'is', 'best']
Length of minimum string is : 2```

Time Complexity: O(N)
Since the above solution has to iterate through the entire list, the time complexity of this approach is O(n) where n is the number of elements in the list.

Auxiliary Space: O(1)
The above solution doesn’t require any extra space. The auxiliary space of this approach is O(1)

Method#4: Using Sort method.

## Python3

 `# Python3 code to demonstrate working of` `# Minimum String length` `# using sort()`   `# initialize list ` `test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``]`   `# printing original list ` `print``(``'The original list :'` `+` `str``(test_list))`   `test_list.sort(key ``=` `len``)` `res ``=` `len``(test_list[``0``])`   `# printing result` `print``(``'Length of minimum string is :'` `+` `str``(res))`   `#this code contributed by tvsk`

Output

```The original list :['gfg', 'is', 'best']
Length of minimum string is :2```

Time Complexity: O( n log n), Where the “sort()” method uses a sorting algorithm called TimSort, which is a combination of Insertion Sort and Merge Sort. Insertion sort has a time complexity of O(n) for small lists and Merge sort has a time complexity of O(n log n) for larger lists.

Auxiliary Space: O(1)

Method #5: Using a loop

You can solve the problem using a simple loop that iterates through each element of the list and keeps track of the minimum length string.

## Python3

 `# Python3 code to demonstrate working of` `# Minimum String length` `# using a loop`   `# Initialize list` `test_list ``=` `[``'gfg'``, ``'is'``, ``'best'``]`   `# Printing original list` `print``(``"The original list : "` `+` `str``(test_list))`   `# Minimum String length` `# using a loop` `min_length ``=` `len``(test_list[``0``])` `for` `i ``in` `range``(``1``, ``len``(test_list)):`   `    ``# If length of our input list lesser thn out min length` `    ``if` `len``(test_list[i]) < min_length:` `        ``min_length ``=` `len``(test_list[i])`   `print``(``"Length of minimum string is : "` `+` `str``(min_length))`   `# This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original list : ['gfg', 'is', 'best']
Length of minimum string is : 2```

Time Complexity: O(n), where n is the length of the input list. We iterate over each element of the list only once.
Auxiliary Space: O(1), we only need to store the minimum length string found so far.

Note: This approach is simple and easy to understand but may not be the most concise or efficient way to solve the problem for large lists.

Article Tags :
Practice Tags :