Given a List that contains only string elements, the following program shows methods of how every other alphabet can be removed from elements except for a specific one and then returns the output.
Input : test_list = [“google”, “is”, “good”, “goggled”, “god”], K = ‘g’
Output : [‘gg’, ”, ‘g’, ‘ggg’, ‘g’]
Explanation : All characters other than “g” removed.
Input : test_list = [“google”, “is”, “good”, “goggled”, “god”], K = ‘o’
Output : [‘oo’, ”, ‘oo’, ‘o’, ‘o’]
Explanation : All characters other than “o” removed.
Method 1: Using loop
In this, we remake the string, by appending only K, and avoiding all other strings from the result.
Python3
# initializing listtest_list = ["google", "is", "good", "goggled", "god"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'g'res = []for sub in test_list: # joining only K characters res.append(''.join([ele for ele in sub if ele == K]))# printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using list comprehension and join()
In this, we perform the task of recreating a list using list comprehension, and then join() can concatenate all occurrences of K.
Python3
# initializing listtest_list = ["google", "is", "good", "goggled", "god"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'g'# appending and joining using list comprehension and join()res = [''.join([ele for ele in sub if ele == K]) for sub in test_list]# printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 : Using count() method.
Iterate over the given list of strings and find the count of the given character in each string and append it to the output list.
Python3
# initializing listtest_list = ["google", "is", "good", "goggled", "god"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'g'res = []for i in test_list: res.append(K*i.count(K))# printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(n), as the count operator takes O(n)
Auxiliary Space: O(n)
Method #4: Using Counter() function
Python3
from collections import Counter# initializing listtest_list = ["google", "is", "good", "goggled", "god"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'g'res = []for i in test_list: freq = Counter(i) res.append(K*freq[K])# printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(n), as the count operator takes O(1) and loop costs O(n)
Auxiliary Space: O(1)
Method 5: using operator.countOf() method
Python3
import operator as op# initializing listtest_list = ["google", "is", "good", "goggled", "god"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'g'res = []for i in test_list: res.append(K*op.countOf(i, K))# printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 5: Using re module (regular expression)
Python3
import re#initializing listtest_list = ["google", "is", "good", "goggled", "god"]#printing original listprint("The original list is : " + str(test_list))#initializing KK = 'g'#using re.findall to find all occurrences of Kres = [''.join(re.findall(K, sub)) for sub in test_list]#printing resultprint("Modified List : " + str(res)) |
The original list is : ['google', 'is', 'good', 'goggled', 'god'] Modified List : ['gg', '', 'g', 'ggg', 'g']
Time Complexity: O(n), as re.findall() takes O(n) to find all occurrences of a character
Auxiliary Space: O(n), as a new list is created with the modified elements
This method uses the re (regular expression) module to search for all occurrences of the specified character (K) in each element of the input list. The re.findall() function is used to find all instances of K in each element, and the join() function is used to concatenate the resulting list into a single string.
