Open In App

Python | Selective Merge in String list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python lists, we can have a problem in which we need to perform the merge operation. There can be various modifications to merge. One such can be replacing every character except a particular character. Let’s see different ways in which this task can be performed. 

Method #1 : Using list comprehension + join() + zip() The combination of above methods can be used to perform this task. In this, we combine the like elements using zip() and merge operation is performed using join(). List comprehension is used to provide logic and iterations. 

Python3




# Python3 code to demonstrate working of
# Selective Merge in String list
# using list comprehension + join() + zip()
 
# initialize lists
test_list1 = ["abc", "de", "efgh"]
test_list2 = ["gfg", "is", "good"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# initialize exempt char
exm_chr = 'g'
 
# Selective Merge in String list
# using list comprehension + join() + zip()
res = [''.join(l if l == exm_chr else k for k, l in zip(i, j))
       for i, j in zip(test_list1, test_list2)]
 
# printing result
print("The resultant list after Selective Merge : " + str(res))


Output : 

The original list 1 : ['abc', 'de', 'efgh']
The original list 2 : ['gfg', 'is', 'good']
The resultant list after Selective Merge : ['gbg', 'de', 'gfgh']

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #2: Using map() + lambda() + join() + zip() The combination of above functions can also be used to perform this task. In this, we use map() and join() to perform task performed by list comprehension. Rest all task is performed similar to above method. 

Python3




# Python3 code to demonstrate working of
# Selective Merge in String list
# using map() + lambda() + join() + zip()
 
# initialize lists
test_list1 = ["abc", "de", "efgh"]
test_list2 = ["gfg", "is", "good"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# initialize exempt char
exm_chr = 'g'
 
# Selective Merge in String list
# using map() + lambda() + join() + zip()
 
 
def temp(ele): return ''.join([i if j != exm_chr else j for i, j in ele])
 
 
res = list(map(temp, (map(lambda k: zip(*k), zip(test_list1, test_list2)))))
 
# printing result
print("The resultant list after Selective Merge : " + str(res))


Output : 

The original list 1 : ['abc', 'de', 'efgh']
The original list 2 : ['gfg', 'is', 'good']
The resultant list after Selective Merge : ['gbg', 'de', 'gfgh']

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #3: Using for loop and zip() 

  1. Initialize two lists test_list1 and test_list2 containing string elements.
  2. Initialize a variable exm_chr with a character that will be exempted from merging.
  3. Initialize an empty list res to store the resultant strings.
  4. Use zip() function to combine the elements of test_list1 and test_list2 based on the index.
  5. Use a loop to iterate through each element in the combined list:
    a. Initialize an empty string temp to store the merged string for the current elements.
    b. Use another loop to iterate through each character of the current element.
    c. If the character in test_list2 for the current position is not equal to exm_chr, append the corresponding character from test_list1 to temp. Otherwise, append the exm_chr.
    d. Append the temp to the res list.
  6. Print the resultant list res.

Python3




test_list1 = ["abc", "de", "efgh"]
test_list2 = ["gfg", "is", "good"]
exm_chr = "g"
 
res = []
for s1, s2 in zip(test_list1, test_list2):
    temp = ""
    for c1, c2 in zip(s1, s2):
        if c2 != exm_chr:
            temp += c1
        else:
            temp += c2
    res.append(temp)
 
print("The resultant list after Selective Merge : ", res)
#This code is contributed by vinay pinjala.


Output

The resultant list after Selective Merge :  ['gbg', 'de', 'gfgh']

Time complexity: O(N*M)

The two outer loops iterate through the input lists, so their time complexity is O(n), where n is the length of the lists.
The two inner loops iterate through the characters in each string, so their time complexity is O(m), where m is the maximum length of a string in the lists.
The overall time complexity of the algorithm is O(n * m), because the outer and inner loops are nested.

Auxiliary Space: O(N*M)

The space used by the input lists is O(n * m) because they contain n strings of maximum length m.
The space used by the output list is also O(n * m) because it contains n strings of maximum length m.
The space used by the temporary string variable temp is O(m) because it can hold one string of maximum length m at a time.
The overall space complexity of the algorithm is O(n * m) because the input and output lists and the temporary string variable all have this space complexity.



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