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
# 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
# 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))
OutputThe 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
# 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))
OutputThe 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.
Similar Reads
Python program to find the occurrence of substring in the string
Given a list of words, extract all the indices where those words occur in the string. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geeks"] Output : [2, 4] Explanation : best and geeks occur at 2nd and 4th index respectively. Input : test_str = 'geeksforgeeks is
4 min read
Python program to remove Nth occurrence of the given word
Given a list of words in Python, the task is to remove the Nth occurrence of the given word in that list. Examples:Â Input: list - ["geeks", "for", "geeks"] word = geeks, N = 2 Output: list - ["geeks", "for"]Input: list - ["can", "you", "can", "a", "can" "?"] word = can, N = 1 Output: list - ["you",
8 min read
Python | Count occurrence of all elements of list in a tuple
Given a tuple and a list as input, write a Python program to count the occurrences of all items of the list in the tuple. Examples: Input : tuple = ('a', 'a', 'c', 'b', 'd') list = ['a', 'b'] Output : 3 Input : tuple = (1, 2, 3, 1, 4, 6, 7, 1, 4) list = [1, 4, 7] Output : 6 Approach #1 : Naive Appro
5 min read
Python Program that prints elements common at specified index of list elements
Given a list of strings, the task is to write a Python program to extract all characters that are same at a specified index of each element of a list. Illustration: Input : test_list = ["geeks", "weak", "beak", "peek"] Output : ['e', 'k'] Explanation : e and k are at same at an index on all strings.
5 min read
Count Occurance of Substring in a List of Strings - Python
To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator
2 min read
Python program to find occurrence to each character in given string
Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let's discuss a few of them. Method #1: Using set() + count() Iterate over the set converted string and get the count
5 min read
Python program to find the frequency of the elements which are common in a list of strings
Given list of strings. The task is to find the frequency of the elements which are common in all strings given in the list of strings. Examples: Input : test_list = ["gegek", "gfgk", "kingg"] Output : {'g': 2, 'k': 1} Explanation : g occurs twice in all Strings. Input : test_list = ["gefgek", "gfgk"
3 min read
Python Program to print strings based on the list of prefix
Given a Strings List, the task here is to write a python program that can extract all the strings whose prefixes match any one of the custom prefixes given in another list. Input : test_list = ["geeks", "peeks", "meeks", "leeks", "mean"], pref_list = ["ge", "ne", "me", "re"] Output : ['geeks', 'meek
5 min read
Python Program to Print Lines Containing Given String in File
In this article, we are going to see how to fetch and display lines containing a given string from a given text file. Assume that you have a text file named geeks.txt saved on the location where you are going to create your python file.Here is the content of the geeks.txt file:Approach:Load the text
2 min read
Python program to split a string by the given list of strings
Given a list of strings. The task is to split the string by the given list of strings. Input : test_str = 'geekforgeeksbestforgeeks', sub_list = ["best"] Output : ['geekforgeeks', 'best', 'forgeeks'] Explanation : "best" is extracted as different list element. Input : test_str = 'geekforgeeksbestfor
4 min read