Open In App

Python – Every Kth Strings Uppercase

Improve
Improve
Like Article
Like
Save
Share
Report

Given a String list, change every Kth string to uppercase.

Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 3 
Output : ['GFG', 'is', 'best', 'FOR', 'geeks'] 
Explanation : All Kth strings are uppercased. 

Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 4 
Output : ['GFG', 'is', 'best', 'for', 'GEEKS'] 
Explanation : All Kth strings are uppercased.

Method #1 : Using loop + upper()

In this, we iterate for all strings using loop and upper is used to perform uppercase, Kth index is detected using modulo operator.

Python3




# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using loop + upper()
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
res = []
for idx in range(len(test_list)):
     
    # checking for Kth index
    if idx % K == 0:
        res.append(test_list[idx].upper())
    else :
        res.append(test_list[idx])
 
# printing result
print("The resultant String list : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']

Time Complexity: O(n), where n is length of test_list.

Auxiliary Space: O(n), where n is length of res list to store the result.

Method #2: Using list comprehension

This is yet another way in which this task can be performed. In this we use list comprehension as shorthand, performs tasks similar to the above method.

Python3




# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using list comprehension
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# shorthand to perform this task
res = [test_list[idx].upper() if idx % K == 0 else test_list[idx]
       for idx in range(len(test_list))]
 
# printing result
print("The resultant String list : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3: Using reduce():
Algorithm:

  1. Initialize the list test_list with the given strings.
  2. Print the original list test_list.
  3. Initialize the value of K to 3.
  4. Define a lambda function to_upper that takes an index and an element as input and returns the uppercase
  5. version of the element if the index is a multiple of K, otherwise returns the original element.
  6. Use the reduce method from the functools module to apply the lambda function to each element of the
  7. test_list and accumulate the results in a list called res.
  8. Print the resulting uppercase string list res.

Python3




# Python3 code to demonstrate working of
# Every Kth Strings Uppercase
# Using reduce method
 
# import reduce from functools module
from functools import reduce
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks", "and", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# define lambda function to convert strings to uppercase
to_upper = lambda idx, elem: elem.upper() if idx % K == 0 else elem
 
# apply lambda function to each element of the list using reduce method
res = reduce(lambda acc, elem: acc + [to_upper(len(acc), elem)], test_list, [])
 
# printing result
print("The resultant String list : " + str(res))
#This code is contributed by Vinay pinjala.


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks', 'and', 'CS']
The resultant String list : ['GFG', 'is', 'best', 'FOR', 'geeks', 'and', 'CS']

Time Complexity: O(n), where n is the length of the input list test_list. The reduce method applies the to_upper lambda function to each element of the test_list exactly once, so the time complexity of the reduce method is O(n).

Space Complexity:  O(n). The test_list and res lists both have a size of n, where n is the length of the input list. The to_upper lambda function requires constant space, so its space complexity is O(1).



Last Updated : 21 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads