Python Program to print strings with repetitive occurrence of an element in a list
Last Updated :
11 May, 2023
Given a strings List, write a Python program that extracts all the strings with more than one occurrence of a specific value(here described using K) in elements of a list.
Examples:
Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 'e'
Output : ['geeksforgeeks', 'geeks']
Explanation : geeks and geeksforgeeks have 2 and 4 occurrences of K respectively.
Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 'k'
Output : ['geeksforgeeks']
Explanation : geeksforgeeks has 2 occurrences of K.
Method 1: Using loop and count()
In this, we check for all the occurrences of K in each string using count, and check if any string has more than 1 occurrence of K, and if found extract that string.
Python3
test_list = [ "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
res = []
for ele in test_list:
if ele.count(K) > 1 :
res.append(ele)
print ( "Repeated K strings : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'best', 'for', 'geeks']
Repeated K strings : ['geeksforgeeks', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension and count()
This is short-hand solution for this task, similar to the above method, just iteration using is done using list comprehension.
Python3
test_list = [ "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
res = [ele for ele in test_list if ele.count(K) > 1 ]
print ( "Repeated K strings : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'best', 'for', 'geeks']
Repeated K strings : ['geeksforgeeks', 'geeks']
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 3: Using replace() and len() methods
Python3
test_list = [ "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
res = []
for ele in test_list:
a = ele
b = ele.replace(K, "")
if ( len (a) - len (b) > = 2 ):
res.append(ele)
print ( "Repeated K strings : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'best', 'for', 'geeks']
Repeated K strings : ['geeksforgeeks', 'geeks']
Method 4: Using a regular expression.
- Import the re module.
- Define the regular expression pattern that matches strings with two or more occurrences of the character K.
- Use the filter() function to apply the regular expression pattern to each string in test_list and return only the strings that match.
- Converting the filtered result to a list and printing it.
Python3
import re
test_list = [ "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
pattern = re. compile (f ".*{K}.*{K}.*" )
res = list ( filter (pattern.match, test_list))
print ( "Repeated K strings : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'best', 'for', 'geeks']
Repeated K strings : ['geeksforgeeks', 'geeks']
Time complexity: O(n), where n is the number of strings in test_list.
Auxiliary space: O(k), where k is the number of strings in test_list that match the regular expression pattern.
Method 5: Using the filter function.
- We start by initializing a list of strings called test_list. The list contains four strings: “geeksforgeeks”, “best”, “for”, and “geeks”.
- We then print the original list using the print() function.
- We initialize a variable K with the value “e”. This is the character that we want to search for in the strings.
- We define a filter function called filter_func using a lambda function. The lambda function takes one argument element which represents an element of the list. The function returns True if the count of the character K in the element is greater than 1. Otherwise, it returns False.
- We use the filter() function to filter out the elements of the list that satisfy the condition specified by the filter function. The filter() function takes two arguments: the filter function and the list to be filtered. We convert the filtered output to a list using the list() function.
- Finally, we print the resulting list of strings that contain the character K more than once using the print() function.
Python3
test_list = [ "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 'e'
filter_func = lambda ele: ele.count(K) > 1
res = list ( filter (filter_func, test_list))
print ( "Repeated K strings : " + str (res))
|
Output
The original list is : ['geeksforgeeks', 'best', 'for', 'geeks']
Repeated K strings : ['geeksforgeeks', 'geeks']
Time complexity: O(n), where n is the length of the list
Auxiliary space: O(k), where k is the number of strings that contain the character K more than once.
Share your thoughts in the comments
Please Login to comment...