Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 11 Dec, 2020
View Discussion
Improve Article
Save Article

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 occurrence 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'
  
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’]

Method 2 : Using list comprehension and count()

This is short hand solution for this task, similar to 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’]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!