Thursday, November 28, 2024
Google search engine
HomeLanguagesPython Program to Removes Every Element From A String List Except For...

Python Program to Removes Every Element From A String List Except For a Specified letter

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'g'
 
res = []
for sub in test_list:
 
    # joining only K characters
    res.append(''.join([ele for ele in sub if ele == K]))
 
# printing result
print("Modified List : " + str(res))


Output

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = '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 result
print("Modified List : " + str(res))


Output

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'g'
 
res = []
for i in test_list:
    res.append(K*i.count(K))
# printing result
print("Modified List : " + str(res))


Output

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'g'
 
res = []
for i in test_list:
    freq = Counter(i)
    res.append(K*freq[K])
# printing result
print("Modified List : " + str(res))


Output

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'g'
 
res = []
for i in test_list:
    res.append(K*op.countOf(i, K))
# printing result
print("Modified List : " + str(res))


Output

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 list
test_list = ["google", "is", "good", "goggled", "god"]
 
#printing original list
print("The original list is : " + str(test_list))
 
#initializing K
K = 'g'
 
#using re.findall to find all occurrences of K
res = [''.join(re.findall(K, sub)) for sub in test_list]
 
#printing result
print("Modified List : " + str(res))


Output

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.

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