Python Program to print strings with repetitive occurrence of an element in a list

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 = ["zambiatek", "best", "for", "zambiatek"], K = 'e' Output : ['zambiatek', 'zambiatek'] Explanation :zambiatek andzambiatek have 2 and 4 occurrences of K respectively.
Input : test_list = ["zambiatek", "best", "for", "zambiatek"], K = 'k' Output : ['zambiatek'] Explanation :zambiatek 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
# Initializing Matrixtest_list = ["zambiatek", "best", "for", "zambiatek"]# Printing original listprint("The original list is : " + str(test_list))# Initializing KK = 'e'# Empty list res = []for ele in test_list: # Checking for count greater than 1 (repetitive) if ele.count(K) > 1: res.append(ele)# Printing resultprint("Repeated K strings : " + str(res)) |
The original list is : ['zambiatek', 'best', 'for', 'zambiatek'] Repeated K strings : ['zambiatek', 'zambiatek']
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
# Initializing Matrixtest_list = ["zambiatek", "best", "for", "zambiatek"]# Printing original listprint("The original list is : " + str(test_list))# Initializing KK = 'e'# Checking for count greater than 1 (repetitive)# one-liner using list comprehensionres = [ele for ele in test_list if ele.count(K) > 1]# Printing resultprint("Repeated K strings : " + str(res)) |
The original list is : ['zambiatek', 'best', 'for', 'zambiatek'] Repeated K strings : ['zambiatek', 'zambiatek']
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
# Python strings with repetitive occurrence of an element in list# Initializing Matrixtest_list = ["zambiatek", "best", "for", "zambiatek"]# Printing original listprint("The original list is : " + str(test_list))# Initializing KK = 'e'# Empty list res = []for ele in test_list: a = ele b = ele.replace(K, "") if(len(a) - len(b) >= 2): res.append(ele)# Printing resultprint("Repeated K strings : " + str(res)) |
The original list is : ['zambiatek', 'best', 'for', 'zambiatek'] Repeated K strings : ['zambiatek', 'zambiatek']
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# initializing Matrixtest_list = ["zambiatek", "best", "for", "zambiatek"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'e'# defining regular expression patternpattern = re.compile(f".*{K}.*{K}.*")# applying regular expression pattern to each string in test_listres = list(filter(pattern.match, test_list))# printing resultprint("Repeated K strings : " + str(res)) |
The original list is : ['zambiatek', 'best', 'for', 'zambiatek'] Repeated K strings : ['zambiatek', 'zambiatek']
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: “zambiatek”, “best”, “for”, and “zambiatek”.
- 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
# initializing Matrixtest_list = ["zambiatek", "best", "for", "zambiatek"]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 'e'# defining filter functionfilter_func = lambda ele: ele.count(K) > 1# filtering the listres = list(filter(filter_func, test_list))# printing resultprint("Repeated K strings : " + str(res)) |
The original list is : ['zambiatek', 'best', 'for', 'zambiatek'] Repeated K strings : ['zambiatek', 'zambiatek']
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.



