Python – Merge consecutive empty Strings
Last Updated :
05 Mar, 2023
Sometimes, while working with python lists, we can have a problem in which we need to perform a merge operation on a string of lists. This merge is consecutive to convert multiple spaces to one. Let’s discuss a certain way in which this can be performed.
Method #1 : Using loop, This is a brute way in which this task can be performed. In this, we find for empty strings using loop and ignore the consecutive strings in making of new list.
Python3
test_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
print ("The original list is : " + str (test_list))
count = 0
res = []
for ele in test_list:
if ele = = '':
count + = 1
if (count % 2 ) = = 0 :
res.append('')
count = 0
else :
res.append(ele)
print (" List after removal of consecutive empty strings : " + str (res))
|
Output :
The original list is : ['Gfg', '', '', '', 'is', '', '', 'best', '']
List after removal of consecutive empty strings : ['Gfg', '', 'is', '', 'best', '']
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 #2 : Using List comprehension
In this method we use list comprehension and by just comparing we can merge consecutive empty strings.
Python3
test_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
print ( "The original list is : " + str (test_list))
res = [test_list[i] for i in range ( len (test_list) - 1 ) if test_list[i]! = test_list[i + 1 ]]
res.append(test_list[ - 1 ])
print ( "List after removal of consecutive empty strings : " + str (res))
|
Output
The original list is : ['Gfg', '', '', '', 'is', '', '', 'best', '']
List after removal of consecutive empty strings : ['Gfg', '', 'is', '', 'best', '']
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using filter() function :
Python3
input_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
output_list = list ( filter ( lambda x: x ! = "", input_list))
print (output_list)
|
Output
['Gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using join() and split()
Python3
test_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
print ( "The original list is : " + str (test_list))
res = ' ' .join(test_list).split()
print ( "List after removal of consecutive empty strings : " + str (res))
|
Output
The original list is : ['Gfg', '', '', '', 'is', '', '', 'best', '']
List after removal of consecutive empty strings : ['Gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using a lambda function and the reduce() method:
Algorithm:
- Import the reduce function from the functools module.
- Initialize the test_list with a list of strings containing empty strings in between some non-empty strings.
- Print the original test_list.
- Use the reduce function to filter out empty strings from the test_list.
- Define a lambda function to reduce the list to a new list, only if the element is not an empty string.
- The reduce function applies the lambda function to each element of the list in turn, and the output of the previous step is used as the input to the next step.
- Finally, the result of the reduce function is printed as the new list containing only non-empty strings.
Python3
from functools import reduce
test_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x + [y] if y ! = '' else x, test_list, [])
print ( "List after removal of consecutive empty strings : " + str (res))
|
Output
The original list is : ['Gfg', '', '', '', 'is', '', '', 'best', '']
List after removal of consecutive empty strings : ['Gfg', 'is', 'best']
Time complexity: The time complexity of this code is O(n), where n is the length of the input list. The join() and split() methods used in the alternative solution have a time complexity of O(n), so the reduce() method has a similar time complexity of O(n).
Auxiliary space complexity: The auxiliary space complexity of this code is O(n), where n is the length of the input list. The reduce() function uses an empty list to store the output of each iteration, which can have a maximum length of n.
Method #6: Using Enumeration
Algorithm:
- Initialize an empty list to store the result.
- Iterate through the list using enumerate(), with index i and word as the value.
- Check if the index is 0 or the previous element is not equal to the current element.
- If the condition is satisfied, append the word to the result list.
- Return the result list.
Python3
test_list = [ 'Gfg' , ' ', ' ', ' ', ' is ', ' ', ' ', ' best ', ' ']
print ( "The original list is : " + str (test_list))
res = [word for i, word in enumerate (test_list) if i = = 0 or test_list[i - 1 ] ! = word]
print ( "List after removal of consecutive empty strings : " + str (res))
|
Output
The original list is : ['Gfg', '', '', '', 'is', '', '', 'best', '']
List after removal of consecutive empty strings : ['Gfg', '', 'is', '', 'best', '']
The time complexity is O(n), where n is the length of the input list. This is because the code iterates through the entire list exactly once to remove the consecutive empty strings. The time taken by the code is directly proportional to the length of the list.
The auxiliary space is O(n). This is because the code creates a new list to store the result, which can be at most the same size as the input list. Therefore, the space taken by the code is directly proportional to the length of the list.
Share your thoughts in the comments
Please Login to comment...