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 listtest_list = [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]  # printing original listprint("The original list is : " + str(test_list))  # initializing K K = 7  # Remove K from Records# Using list comprehensionres = [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 listtest_list = [(5, 6, 7), (2, 5), (1, ), (7, 8), (9, 7, 2, 1)]  # printing original listprint("The original list is : " + str(test_list))  # initializing K K = 7  # Remove K from Records# Using filter() + lambdares = [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)]

My Personal Notes arrow_drop_up