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
test_list = [( 5 , 6 , 7 ), ( 2 , 5 ), ( 1 , ), ( 7 , 8 ), ( 9 , 7 , 2 , 1 )]
print ("The original list is : " + str (test_list))
K = 7
res = [ tuple (ele for ele in sub if ele ! = K) for sub in test_list]
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
test_list = [( 5 , 6 , 7 ), ( 2 , 5 ), ( 1 , ), ( 7 , 8 ), ( 9 , 7 , 2 , 1 )]
print ("The original list is : " + str (test_list))
K = 7
res = [ tuple ( filter ( lambda ele: ele ! = 0 , sub)) for sub in test_list]
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
def remove_k_from_records(test_list, K):
modified_list = list ( map ( lambda tpl: tuple ( filter ( lambda x: x ! = K, tpl)), test_list))
return modified_list
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 - Replace Non-Maximum Records
Sometimes, while working with Python records, we can have a problem in which we need to perform replace of all the records whose one element is not Maximum. This kind of problem can have application in many domains including day-day programming and web development domain. Let's discuss certain ways
4 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
Python - Convert Uneven Lists into Records
Sometimes, while working with Records, we can have a problem, that we have keys in one list and values in other. But sometimes, values can be multiple in order, like the scores or marks of particular subject. This type of problem can occur in school programming and development domains. Lets discuss
3 min read
Python - Remove List Item
Removing List Item can be achieved using several built-in methods that provide flexibility in how you remove list items. In this article, we'll explore the different ways to remove list items in Python. Removing Item by Value with remove()The remove() method allows us to remove the first occurrence
3 min read
Python - Remove Rear K characters from String List
Sometimes, we come across an issue in which we require to delete the last characters from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plu
5 min read