Python – Retain records with N occurrences of K

Somtimes, while working with Python tuples list, we can have a problem in which we need to perform retention of all the records where occurrences of K is N times. This kind of problem can come in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 2
Output : [(4, 5, 5, 4)]

Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 3
Output : []

Method #1 : Using list comprehension + count()
The combination of above functions can be used to solve this problem. In this, we perform the task of counting occurrences and conditions and iterations using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Retain records with N occurrences of K
# Using count() + list comprehension
  
# initializing list
test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K
K = 4
  
# initializing N 
N = 3
  
# Retain records with N occurrences of K
# Using count() + list comprehension
res = [ele for ele in test_list if ele.count(K) == N]
  
# printing result 
print("Filtered tuples : " + str(res)) 

chevron_right


Output :



The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]

 

Method #2 : Using list comprehension + sum()
The combination of above functions can be used to solve this problem. In this, we perform the task of computing summation count of K using sum().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Retain records with N occurrences of K
# Using list comprehension + sum()
  
# initializing list
test_list = [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K
K = 4
  
# initializing N 
N = 3
  
# Retain records with N occurrences of K
# Using list comprehension + sum()
res = [ele for ele in test_list if sum(cnt == K for cnt in ele) == N]
  
# printing result 
print("Filtered tuples : " + str(res)) 

chevron_right


Output :

The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)]
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.