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 expression, The combination of above functionalities can be used to perform this task. In this, we extract all the list lengths using generator expression and return maximum of them using max().
Python3
# Python3 code to demonstrate working of # Extracting length of longest string in list # using max() + generator expression # initialize list test_list = [ 'gfg' , 'is' , 'best' ] # printing original list print ("The original list : " + str (test_list)) # Extracting length of longest string in list # using max() + generator expression res = max ( len (ele) for ele in test_list) # printing result print ("Length of maximum string is : " + str (res)) |
The original list : ['gfg', 'is', 'best'] Length of maximum string is : 4
Time Complexity: O(n), where n is the length of the input list. This is because we’re using max() + generator expression which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #2 : Using len() + key argument + max() The combination of above functions can be used to perform this task. In this, we extract the maximum length using len() and max(). It is faster than above method as it performs more task built in rather than overhead by generator expression.
Python3
# Python3 code to demonstrate working of # Extracting length of longest string in list # using len() + key argument + max() # initialize list test_list = [ 'gfg' , 'is' , 'best' ] # printing original list print ("The original list : " + str (test_list)) # Extracting length of longest string in list # using len() + key argument + max() res = len ( max (test_list, key = len )) # printing result print ("Length of maximum string is : " + str (res)) |
The original list : ['gfg', 'is', 'best'] Length of maximum string is : 4
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space required
Method 3: Alternative method
Python3
lst = [ 'gfg' , 'is' , 'best' ] max_length,longest_element = max ([( len (x),x) for x in (lst)]) print ( "max length is: " ,max_length) print ( "longest string is: " , longest_element) |
max length is: 4 longest string is: best
Method 4: Using the enumerate function
Python3
lst = [ 'gfg' , 'is' , 'best' ] max_length,longest_element = max ([( len (x),x) for a,x in enumerate (lst)]) print ( "max length is: " ,max_length) print ( "longest string is: " , longest_element) |
max length is: 4 longest string is: best
Method 5: Using the reduce function
One unique approach to extract the maximum length of all strings in a list is using the built-in function “reduce” from the “functools” module. The reduce function applies a function cumulatively to the elements of an iterable and returns a single output. In this case, we can use the function “max” as the cumulative function and the iterable is a list of the lengths of each string in the original list.
Here is an example of how to use the reduce function to extract the maximum length of all strings in a list:
Python3
from functools import reduce # initialize list test_list = [ 'gfg' , 'is' , 'best' ] # Extracting length of longest string in list using reduce function res = reduce ( lambda x, y: max (x, y), map ( len , test_list)) # printing result print ( "Length of maximum string is : " + str (res)) #this code is contributed by edula vinay kumar reddy |
Length of maximum string is : 4
Time complexity: O(n)
Auxiliary Space: O(n)
Method 6: Using the list comprehension:
Python3
# Initialize the list of strings test_list = [ 'gfg' , 'is' , 'best' ] # Use a list comprehension to calculate the length of each string # in the list and store the results in a new list string_lengths = [ len (i) for i in test_list] # Find the maximum length from the list of string lengths max_length = max (string_lengths) # Print the result print ( "Length of maximum string is :" , max_length) |
Length of maximum string is : 4
Time complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...