Open In App

Python – Fill Strings for size K in Tuple List

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes while working with Tuple lists, we can have a problem in which we need to perform filling of strings to complete certain size in lists. This type of ask can occur in data domains and data preprocessing. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(‘Gfg’, ‘is’), (‘best’, ‘for’), (‘CS’, ‘Geeks’)], K = 6, fill_char = ‘#’ 
Output : [(‘Gfg###’, ‘is####’), (‘best##’, ‘for###’), (‘CS####’, ‘Geeks#’)] 

Input : test_list = [(‘Gfg’, ‘is’), (‘best’, ‘for’), (‘CS’, ‘Geeks’)], K = 5, fill_char = ‘!’ 
Output : [(‘Gfg!!’, ‘is!!!’), (‘best!’, ‘for!!’), (‘CS!!!’, ‘Geeks’)]

Method #1 : Using list comprehension + len() This functionality is shorthand to brute force method that can be applied to solve this problem. In this, we perform task of checking for K size using len(), and perform required fill by a character. 

Python3




# Python3 code to demonstrate working of
# Fill Strings for size K in Tuple List
# Using list comprehension + len()
 
# initializing list
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 8
 
# initializing fill_char
fill_char = '*'
 
# Fill Strings for size K in Tuple List
# Using list comprehension + len()
res = [(a + fill_char * (K - len(a)), b + fill_char * (K - len(b))) for a, b in test_list]
 
# printing result
print("The modified list : " + str(res))


Output : 

The original list is : [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
The modified list : [('Gfg*****', 'is******'), ('best****', 'for*****'), ('CS******', 'Geeks***')]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

  Method #2 : Using list comprehension + ljust() The combination of above functions can be used to solve this problem. In this, we perform the task of filling trailing characters using ljust(). 

Python3




# Python3 code to demonstrate working of
# Fill Strings for size K in Tuple List
# Using list comprehension + ljust()
 
# initializing list
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 8
 
# initializing fill_char
fill_char = '*'
 
# Fill Strings for size K in Tuple List
# Using list comprehension + ljust()
res = [(a.ljust(K, fill_char), b.ljust(K, fill_char)) for a, b in test_list]
 
# printing result
print("The modified list : " + str(res))


Output : 

The original list is : [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
The modified list : [('Gfg*****', 'is******'), ('best****', 'for*****'), ('CS******', 'Geeks***')]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3 : Using Lambda Function and Map

Approach

The approach here is to use the map() function to apply a lambda function to each string in each tuple in the input list. The lambda function concatenates the fill character with the string until the resulting string has a length of K. The resulting map objects are then converted back to tuples and returned as a list of tuples.

Algorithm

1. Define a lambda function fill_func that takes a string and returns the string concatenated with the fill character up to the size of K.
2. Use a list comprehension to iterate through each tuple in the input list test_list.
3. Use the map() function to apply the fill_func lambda function to each string in the current tuple tpl, and convert the resulting map object to a tuple.
4. Append the resulting tuple to a list result.
5. Return the list of tuples with the filled strings.

Python3




def fill_strings_map(test_list, K, fill_char):
    # Define a lambda function that takes a string and returns the string concatenated with the fill character up to the size of K
    fill_func = lambda string: string + fill_char * (K - len(string))
    # Use the map() function to apply the lambda function to each string in each tuple in the list, and convert the resulting map objects to tuples
    result = [tuple(map(fill_func, tpl)) for tpl in test_list]
    # Return the list of tuples with the filled strings
    return result
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
K=5
fill_char='!'
print(fill_strings_map(test_list, K, fill_char))


Output

[('Gfg!!', 'is!!!'), ('best!', 'for!!'), ('CS!!!', 'Geeks')]

 Time complexity: O(nmk), where n is the number of tuples in the input list, m is the number of strings in each tuple, and k is the length of the longest string in the input list. However, in practice, the function is likely to have a much lower time complexity, as most input lists will be small and have relatively short strings.

Auxiliary Space: O(nmk), where n, m, and k are as defined above. Additionally, the function creates a new lambda function, which has a space complexity of O(1). However, in practice, the lambda function is likely to have a negligible space complexity compared to the size of the input list.

Method #4 :Using nested loops and string concatenation:

Algorithm:

1.Iterate over each tuple in the given list.
2.For each tuple, get the two strings.
3.Check the length of each string.
4.If the length is less than K, append fill_char to the string until the length becomes equal to K.
5.Append the modified tuple to the result list.
6.Return the result list.

Python3




# Python3 code to demonstrate working of
# Fill Strings for size K in Tuple List
# Using for loop
 
# initializing list
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 8
 
# initializing fill_char
fill_char = '*'
 
# Fill Strings for size K in Tuple List
# Using for loop
res = []
for tpl in test_list:
    s1 = tpl[0]
    s2 = tpl[1]
    if len(s1) < K:
        s1 += fill_char * (K - len(s1))
    if len(s2) < K:
        s2 += fill_char * (K - len(s2))
    res.append((s1, s2))
 
# printing result
print("The modified list : " + str(res))
# This code is contributed by Jyothi pinjala.


Output

The original list is : [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
The modified list : [('Gfg*****', 'is******'), ('best****', 'for*****'), ('CS******', 'Geeks***')]

Time Complexity: O(N*K), where N is the number of tuples in the input list and K is the length of the strings to be filled.

Auxiliary Space: O(N*K), the space required to store the modified tuples in the result list.

Method#5: Using Recursive method.

In the recursive case, the method takes the first tuple in the list and extracts the two strings a and b. It then constructs a new tuple with a and b filled up to size K using the fill_char character. This new tuple is then concatenated with the result of recursively calling the method with the rest of the list.

Note that this method is less efficient than the list comprehension method because it uses recursion and creates new tuples for each element. However, it demonstrates an alternative approach to solving the problem using recursion.

Python3




def fill_strings_recursive(test_list, K, fill_char):
    if not test_list:
        return []
    a, b = test_list[0]
    return [(a + fill_char * (K - len(a)), b + fill_char * (K - len(b)))] + fill_strings_recursive(test_list[1:], K, fill_char)
# initializing list
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 8
 
# initializing fill_char
fill_char = '*'
res = fill_strings_recursive(test_list,K,fill_char)
 
# printing result
print("The modified list : " + str(res))


Output

The original list is : [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
The modified list : [('Gfg*****', 'is******'), ('best****', 'for*****'), ('CS******', 'Geeks***')]

Time complexity:

Best case: O(1) when the input list is empty.
Worst case: O(n*K) where n is the length of the input list and K is the size to fill strings up to. This is because the method needs to iterate over each tuple in the list, and for each tuple it needs to construct a new tuple with filled strings up to size K. The operation of filling a string takes O(K) time.
Auxiliary Space:

Best case: O(1) when the input list is empty.
Worst case: O(n*K) where n is the length of the input list and K is the size to fill strings up to. This is because the method needs to create a new tuple for each tuple in the input list with filled strings up to size K. The space needed to store a tuple with filled strings is proportional to K.

Method#6: Using re module

Algorithm:

  1. Initialize the original list of tuples.
  2. Set the value of K to the desired length of the strings.
  3. Set the fill character to the character that will be used to fill the strings.
  4. Using list comprehension, iterate over the list of tuples.
  5. For each tuple, apply the zip_longest() function from the itertools module to the two strings, left-justifying them with the fill character to a length of K.
  6. Concatenate the two formatted strings using the format() method with the appropriate format string.
  7. Return the modified list of tuples.

Python3




import re
 
test_list = [('Gfg', 'is'), ('best', 'for'), ('CS', 'Geeks')]
K = 8
fill_char = '*'
 
filler = fill_char * (K - 1# create a string of K-1 fill_char characters
 
res = [tuple(re.sub(fr'\b(\w{{1,{K}}})\b', fr'\1{filler}', s) for s in tpl) for tpl in test_list]
print(res)


Output

[('Gfg*******', 'is*******'), ('best*******', 'for*******'), ('CS*******', 'Geeks*******')]

Time Complexity:
The time complexity of this approach is O(n), where n is the number of tuples in the list. The zip_longest() function has a time complexity of O(max(len(a), len(b))), where a and b are the strings being zipped. Since the strings are padded to a fixed length of K, the zip_longest() function will have a time complexity of O(K) for each tuple. Therefore, the overall time complexity of the algorithm is O(n*K).

Auxiliary Space Complexity:
The auxiliary space complexity of this approach is O(n*K), where n is the number of tuples in the list and K is the length of the padded strings. This is because the modified list of tuples is stored in memory, and each string in the tuples has a maximum length of K.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads