Skip to content
Related Articles

Related Articles

Improve Article
Python – Remove K from Records
  • Last Updated : 22 Jun, 2020

Sometimes, while working with Python tuples, we can have a problem in which we need to remove all K from lists. This task can have application in many domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input :
test_list = [(5, 6, 7), (2, 5, 7)]
K = 7
Output : [(5, 6), (2, 5)]

Input :
test_list = [(5, 6, 7), (2, 5, 7)]
K = 5
Output : [(6, 7), (2, 7)]

Method #1 : Using list comprehension
This is one of the way in which this task can be performed. In this, we perform the task of getting conditional checks and recreating new list using list comprehension.




# Python3 code to demonstrate working of 
# Remove K from Records
# Using list comprehension
  
# initializing list
test_list = [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 7
  
# Remove K from Records
# Using list comprehension
res = [tuple(ele for ele in sub if ele != K) for sub in test_list]
  
# printing result 
print("The records after removing K : " + str(res)) 
Output :



The original list is : [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]
The records after removing K : [(5, 6), (2, 5), (1, ), (8, ), (9, 2, 1)]

 

Method #2 : Using filter() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of removing all Ks using filter() and lambda functionality.




# Python3 code to demonstrate working of 
# Remove K from Records
# Using filter() + lambda
  
# initializing list
test_list = [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 7
  
# Remove K from Records
# Using filter() + lambda
res = [tuple(filter(lambda ele: ele != 0, sub)) for sub in test_list]
  
# printing result 
print("The records after removing K : " + str(res)) 
Output :
The original list is : [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]
The records after removing K : [(5, 6), (2, 5), (1, ), (8, ), (9, 2, 1)]

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :