Python Program to Removes Every Element From A String List Except For a Specified letter
Last Updated :
18 Feb, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python Program to remove a specific digit from every element of the list Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrenc
7 min read
Python program for removing i-th character from a string In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing.Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude t
2 min read
Python program to remove last N characters from a string In this article, weâll explore different ways to remove the last N characters from a string in Python. This common string manipulation task can be achieved using slicing, loops, or built-in methods for efficient and flexible solutions.Using String SlicingString slicing is one of the simplest and mos
2 min read
Python Program to Remove Palindromic Elements from a List Given a list, the task here is to write Python programs that can remove all the elements which have palindromic equivalent element present in the list. Examples: Input : test_list = [54, 67, 12, 45, 98, 76, 9] Output : [12, 98] Explanation : 67 has 76 as palindromic element, both are omitted. Input
2 min read
Find the List elements starting with specific letter - Python The goal is to filter the elements of a list that start with a specific letter, such as 'P'. For example, in the list ['Python', 'Java', 'C++', 'PHP'], we aim to find the elements that begin with the letter 'P', resulting in the filtered list ['Python', 'PHP']. Let's explore different approaches to
3 min read