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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...