Given a String list, the task is to write a Python program to convert uppercase strings if the length is greater than K.
Examples:
Input : test_list = ["Gfg", "is", "best", "for", "Lazyroar"], K = 3 Output : ['Gfg', 'is', 'BEST', 'for', 'GEEKS'] Explanation : Best has 4 chars, hence BEST is uppercased.
Input : test_list = ["Gfg", "is", "best", "for", "Lazyroar"], K = 4 Output : ['Gfg', 'is', 'best', 'for', 'GEEKS'] Explanation : Lazyroar has 5 chars [greater than 4], hence GEEKS is uppercased.
Method #1 : Using upper() + loop
In this, we perform the task of uppercasing using upper(), and conditional statements for greater are checked using a loop.
Python3
# Python3 code to demonstrate working of # Conditional Uppercase by size # Using upper() + loop # initializing list test_list = [ "Gfg" , "is" , "best" , "for" , "Lazyroar" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 3 res = [] for ele in test_list: # check for size if len (ele) > K: res.append(ele.upper()) else : res.append(ele) # printing result print ( "Modified Strings : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Lazyroar'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
In this, the task of iteration is performed inside list comprehension to act as shorthand to the similar method as above.
Python3
# Python3 code to demonstrate working of # Conditional Uppercase by size # Using list comprehension # initializing list test_list = [ "Gfg" , "is" , "best" , "for" , "Lazyroar" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 3 # list comprehension for one liner solution res = [ele.upper() if len (ele) > K else ele for ele in test_list] # printing result print ( "Modified Strings : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Lazyroar'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using map function + lambda function.
Python3
# Python3 code to demonstrate working of # Conditional Uppercase by size # using map function and lambda function # Initializing list test_list = [ "Gfg" , "is" , "best" , "for" , "Lazyroar" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 3 # Using map and lambda function res = list ( map ( lambda ele: ele.upper() if len (ele) > K else ele, test_list)) # Printing result print ( "Modified Strings : " + str (res)) # This code is contributed by Edula Vinay Kumar Reddy |
The original list is : ['Gfg', 'is', 'best', 'for', 'Lazyroar'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using map() and a named helper function
- Define a named function ‘conditional_uppercase’ that takes a string as an input parameter
- Inside the function, check if the length of the string is greater than ‘K’
- If yes, return the uppercase version of the string
- If no, return the original string.
- Initialize an empty list called ‘res’
- Use map() function to apply the ‘conditional_uppercase’ function to each element of the ‘test_list’
- Convert the map object to a list using the list() function and store it in ‘res’
- Print the modified strings stored in ‘res’
Python3
# Python3 code to demonstrate working of # Conditional Uppercase by size # Using map() and a named function # initializing list test_list = [ "Gfg" , "is" , "best" , "for" , "Lazyroar" ] # Printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 3 # named function for conditional uppercase def conditional_uppercase(ele): return ele.upper() if len (ele) > K else ele # Applying conditional_uppercase function to each element # map function res = list ( map (conditional_uppercase, test_list)) # Printing the result print ( "Modified Strings : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Lazyroar'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time complexity: O(n), where n is the number of elements in the ‘test_list’
Auxiliary space: O(n), as we are storing the modified strings in a new list ‘res’
Method #6: Using generator function
Python3
# Python3 code to demonstrate working of # Conditional Uppercase by size # Using a generator function # Initializing list test_list = [ "Gfg" , "is" , "best" , "for" , "Lazyroar" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing K K = 3 # Generator function for conditional uppercase def conditional_uppercase_generator(lst, k): for ele in lst: yield ele.upper() if len (ele) > k else ele # Creating a generator object gen = conditional_uppercase_generator(test_list, K) # Converting generator object to list res = list (gen) # Printing the result print ( "Modified Strings : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Lazyroar'] Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time complexity: O(n), since we need to iterate over all the elements in the list.
Auxiliary space: O(1) because we are not storing all the modified strings in a separate list. Instead, we generate them on-the-fly using the generator function.