Open In App

Python – Length of shortest string in string list

Last Updated : 05 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Similar Reads

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() + loop + items() The combination of above functions
4 min read
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: C/C++ Code string = &quot;Geeksforgeeks&quot; print(len(string)) Output13String len() Syntax len(string) Parameter String: string of which you want to find the length. Return: It returns
3 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python | Find maximum length sub-list in a nested list
Given a list of lists, write a Python program to find the list with maximum length. The output should be in the form (list, list_length). Examples: Input : [['A'], ['A', 'B'], ['A', 'B', 'C']] Output : (['A', 'B', 'C'], 3) Input : [[1, 2, 3, 9, 4], [5], [3, 8], [2]] Output : ([1, 2, 3, 9, 4], 5) Let's discuss different approaches to solve this prob
3 min read
Python | Convert 1D list to 2D list of variable length
Given a 1D list 'lst' and list of variable lengths 'var_lst', write a Python program to convert the given 1D list to 2D list of given variable lengths. Examples: Input : lst = [1, 2, 3, 4, 5, 6] var_lst = [1, 2, 3] Output : [[1], [2, 3], [4, 5, 6]] Input : lst = ['a', 'b', 'c', 'd', 'e'] var_lst = [3, 2] Output : [['a', 'b', 'c'], ['d', 'e']] Metho
7 min read
Python Program For Finding The Length Of Longest Palindrome List In A Linked List Using O(1) Extra Space
Given a linked list, find the length of the longest palindrome list that exists in that linked list. Examples: Input : List = 2-&gt;3-&gt;7-&gt;3-&gt;2-&gt;12-&gt;24 Output : 5 The longest palindrome list is 2-&gt;3-&gt;7-&gt;3-&gt;2 Input : List = 12-&gt;4-&gt;4-&gt;3-&gt;14 Output : 2 The longest palindrome list is 4-&gt;4 Recommended: Please sol
3 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python Program for Dijkstra's shortest path algorithm | Greedy Algo-7
Given a graph and a source vertex in the graph, find the shortest paths from source to all vertices in the given graph.Dijkstra's algorithm is very similar to Prim's algorithm for minimum spanning tree. Like Prim's MST, we generate an SPT (shortest path tree) with a given source as root. We maintain two sets, one set contains vertices included in t
4 min read
Shortest Path Problem Between Routing Terminals - Implementation in Python
The famous Dijkstra's algorithm can be used in a variety of contexts - including as a means to find the shortest route between two routers, also known as Link state routing. This article explains a simulation of Dijkstra's algorithm in which the nodes (routers) are terminals. Once the shortest path between two nodes (terminals) is calculated, the s
6 min read
Building an undirected graph and finding shortest path using Dictionaries in Python
Prerequisites: BFS for a GraphDictionaries in Python In this article, we will be looking at how to build an undirected graph and then find the shortest path between two nodes/vertex of that graph easily using dictionaries in Python Language. Building a Graph using Dictionaries Approach: The idea is to store the adjacency list into the dictionaries,
3 min read
Practice Tags :