Python - Remove K from Records
Last Updated :
10 Apr, 2023
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
# 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)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
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
# 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)]
Time complexity: O(n*m), because we are applying filter() function to each tuple in test_list.
Auxiliary Space: O(n*m), because we are applying filter() function to each tuple in test_list
Method #3 : Using list+map()+filter()+lambda
Approach
we use the map() function along with a lambda function to remove the unwanted element K from each tuple in the list. We iterate over the tuples in the list using map(), apply the filter() function to each tuple using the lambda function, and finally convert the resulting filter object to a tuple using the tuple() constructor. We use the list() constructor to convert the resulting map object to a list. Finally, we return the modified list.
Algorithm
1.Define a function remove_k_from_records(test_list, K) that takes a list of tuples test_list and a value K as input.
2. Use map() function to apply a lambda function to each element of the list test_list.
3. The lambda function takes each tuple and removes K from it using filter() function.
4. Use filter() function to remove elements from the tuple if they are equal to K.
5. The resulting filter object is converted to a tuple using the tuple() constructor.
6. The resulting map object is converted to a list using the list() constructor.
7. Return the modified list.
Python3
# Define a function to remove K from records in a list of tuples
def remove_k_from_records(test_list, K):
# Use map and lambda to remove K from each tuple in the list
# map applies a lambda function to each element of the list and returns a map object
# lambda function takes a tuple and returns a new tuple with K removed using filter function
# filter removes elements from the tuple if they are equal to K
# tuple constructor converts the resulting filter object to a tuple
# list constructor converts the resulting map object to a list
modified_list = list(map(lambda tpl: tuple(filter(lambda x: x != K, tpl)), test_list))
# Return the modified list
return modified_list
# Test the function with the given inputs
test_list = [(5, 6, 7), (2, 5, 7)]
K = 7
result = remove_k_from_records(test_list, K)
print(result)
Time complexity: O(n * m), because we are applying filter() function to each tuple in test_list.
Auxiliary Space: O(n * m), because we are applying filter() function to each tuple in test_list
Similar Reads
Python - Remove nested records from tuple Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Letâs discuss certain way in which this task c
5 min read
Python - Remove Record if Nth Column is K Sometimes while working with a list of records, we can have a problem in which we need to perform the removal of records on the basis of the presence of certain elements at the Nth position of the record. Let us discuss certain ways in which this task can be performed. Method #1: Using loop This is
10 min read
Python - Remove None Nested Records Sometimes, while working with Python Records, can have problem in which we need to perform the removal of data which have all key's values as None in nested records. This kind of problem can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method
4 min read
Python - Remove records if Key not present Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove all the dictionaries in which a particular key is not present. This kind of problem can have applications in many domains such as day-day programming and data domain. Let's discuss certain ways in whi
6 min read
Python - Remove Consecutive K element records Sometimes, while working with Python records, we can have a problem in which we need to remove records on the basis of presence of consecutive K elements in tuple. This kind of problem is peculiar but can have applications in data domains. Let's discuss certain ways in which this task can be perform
7 min read
Python | Remove Front K elements We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read