Open In App

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

Last Updated : 11 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Initializing Matrix
test_list = ["geeksforgeeks", "best", "for", "geeks"]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = '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 result
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




# Initializing Matrix
test_list = ["geeksforgeeks", "best", "for", "geeks"]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 'e'
 
# Checking for count greater than 1 (repetitive)
# one-liner using list comprehension
res = [ele for ele in test_list if ele.count(K) > 1]
 
# Printing result
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




# Python strings with repetitive occurrence of an element in list
 
# Initializing Matrix
test_list = ["geeksforgeeks", "best", "for", "geeks"]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = '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 result
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.

  1. Import the re module.
  2. Define the regular expression pattern that matches strings with two or more occurrences of the character K.
  3. Use the filter() function to apply the regular expression pattern to each string in test_list and return only the strings that match.
  4. Converting the filtered result to a list and printing it.

Python3




import re
 
# initializing Matrix
test_list = ["geeksforgeeks", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'e'
 
# defining regular expression pattern
pattern = re.compile(f".*{K}.*{K}.*")
 
# applying regular expression pattern to each string in test_list
res = list(filter(pattern.match, test_list))
 
# printing result
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.

  1. We start by initializing a list of strings called test_list. The list contains four strings: “geeksforgeeks”, “best”, “for”, and “geeks”.
  2. We then print the original list using the print() function.
  3. We initialize a variable K with the value “e”. This is the character that we want to search for in the strings.
  4. 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.
  5. 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.
  6. Finally, we print the resulting list of strings that contain the character K more than once using the print() function.

Python3




# initializing Matrix
test_list = ["geeksforgeeks", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 'e'
 
# defining filter function
filter_func = lambda ele: ele.count(K) > 1
 
# filtering the list
res = list(filter(filter_func, test_list))
 
# printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads