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", "geeks"], K = 3
Output : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Explanation : Best has 4 chars, hence BEST is uppercased.
Input : test_list = ["Gfg", "is", "best", "for", "geeks"], K = 4
Output : ['Gfg', 'is', 'best', 'for', 'GEEKS']
Explanation : geeks 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
test_list = [ "Gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 3
res = []
for ele in test_list:
if len (ele) > K:
res.append(ele.upper())
else :
res.append(ele)
print ( "Modified Strings : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
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
test_list = [ "Gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 3
res = [ele.upper() if len (ele) > K else ele for ele in test_list]
print ( "Modified Strings : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Modified Strings : ['Gfg', 'is', 'BEST', 'for', 'GEEKS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using map function + lambda function.
Python3
test_list = [ "Gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( map ( lambda ele: ele.upper() if len (ele) > K else ele, test_list))
print ( "Modified Strings : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
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
test_list = [ "Gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 3
def conditional_uppercase(ele):
return ele.upper() if len (ele) > K else ele
res = list ( map (conditional_uppercase, test_list))
print ( "Modified Strings : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
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
test_list = [ "Gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 3
def conditional_uppercase_generator(lst, k):
for ele in lst:
yield ele.upper() if len (ele) > k else ele
gen = conditional_uppercase_generator(test_list, K)
res = list (gen)
print ( "Modified Strings : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 May, 2023
Like Article
Save Article