Open In App

Python – Remove K from Records

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

[(5, 6), (2, 5)]

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
Previous
Next
Share your thoughts in the comments

Similar Reads