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
OutputThe 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
OutputThe 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
OutputThe 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.
Similar Reads
How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort()
2 min read
Python - Longest Substring Length of K Given a String and a character K, find longest substring length of K. Input : test_str = 'abcaaaacbbaa', K = b Output : 2 Explanation : b occurs twice, 2 > 1. Input : test_str = 'abcaacccbbaa', K = c Output : 3 Explanation : Maximum times c occurs is 3. Method #1: Using loop This is brute way to
7 min read
Python | Split list of strings into sublists based on length Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'geeksforgeeks'] Output : [['to'], ['Welcome'], ['geeksforgeeks']
3 min read
Python - Words Lengths in String We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List
2 min read
Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop.
2 min read
Python - Keys with shortest length lists in dictionary Sometimes, while working with Python lists, we can have problem in which we need to return the keys which have minimum lengths of lists as values. This can have application in domains in which we work with data. Lets discuss certain ways in which this task can be performed. Method #1: Using len() +
4 min read
Python | Average String length in list Sometimes, while working with data, we can have a problem in which we need to gather information of average length of String data in list. This kind of information might be useful in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehen
8 min read
Python - Smallest Length String To identify the smallest string by length in a list, we compare the lengths of all strings and select the shortest one.Using min()The most efficient way to do this is by using the min() function with the key parameter. This method directly calculates the smallest string based on its length, avoiding
2 min read
Python | Extract length of longest string in list Sometimes, while working with a lot of data, we can have a problem in which we need to extract the maximum length of all the strings in list. This kind of problem can have application in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using max() + generator
4 min read
Python - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read