Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Merge consecutive empty Strings

Python – Merge consecutive empty Strings

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




# Python3 code to demonstrate
# Merge consecutive empty Strings
# using loop
 
# Initializing list
test_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Merge consecutive empty Strings
# using loop
count = 0
res = []
for ele in test_list:
    if ele =='':
        count += 1
        if (count % 2)== 0:
            res.append('')
            count = 0
    else:
        res.append(ele)
 
# printing result
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




# input list
test_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
print("The original list is : " + str(test_list))
# list comprehension
res= [test_list[i] for i in range(len(test_list)-1) if test_list[i]!=test_list[i+1]]
# appending last word in the list because we are checking upto n-1
res.append(test_list[-1])
print ("List after removal of consecutive empty strings : " + str(res))
# this code is contributed Asif_shaik


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




# Initialize input list
input_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
# Use filter function to filter out empty strings
output_list = list(filter(lambda x: x != "", input_list))
# Print the output list
print(output_list)
#This code is contributed by pinjala Jyothi.


Output

['Gfg', 'is', 'best']

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #4 : Using join() and split()
 

Python3




# Initializing list
test_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
# printing original list
print ("The original list is : " +  str(test_list))
# Using join() and split()
res = ' '.join(test_list).split()
# printing result
print ("List after removal of consecutive empty strings : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Import the reduce function from the functools module.
  2. Initialize the test_list with a list of strings containing empty strings in between some non-empty strings.
  3. Print the original test_list.
  4. Use the reduce function to filter out empty strings from the test_list.
  5. Define a lambda function to reduce the list to a new list, only if the element is not an empty string.
  6. 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.
  7. Finally, the result of the reduce function is printed as the new list containing only non-empty strings.

Python3




from functools import reduce
# Initializing list
test_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
# printing original list
print ("The original list is : " +  str(test_list))
# Using join() and split()
res = reduce(lambda x, y: x + [y] if y != '' else x, test_list, [])
# printing result
print ("List after removal of consecutive empty strings : " + str(res))
#This code is contributed by tvsk.


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:

  1. Initialize an empty list to store the result.
  2. Iterate through the list using enumerate(), with index i and word as the value.
  3. Check if the index is 0 or the previous element is not equal to the current element.
  4. If the condition is satisfied, append the word to the result list.
  5. Return the result list.

Python3




#input list
test_list = ['Gfg', '', '', '', 'is', '', '', 'best', '']
print("The original list is : " + str(test_list))
 
#using enumeration
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))
#This code is contributed by Vinay Pinjala.


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments