![]() |
VOOZH | about |
Sometimes, while working with Python tuples list, we can have a problem in which we need to perform retention of all the records where occurrences of K is N times. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 2
Output : [(4, 5, 5, 4)]
Input : test_list = [(4, 5, 5, 4), (5, 4, 3)], K = 5, N = 3
Output : []
Method #1 : Using list comprehension + count()
The combination of above functions can be used to solve this problem. In this, we perform the task of counting occurrences and conditions and iterations using list comprehension.
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension + sum()
The combination of above functions can be used to solve this problem. In this, we perform the task of computing summation count of K using sum().
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method#3:Using count() + Recursive function
Algorithm:
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity: O(n), where n is the length of the input list. This is because the function recursively traverses the list once and performs a constant amount of work for each tuple.
Auxiliary space: O(n), where n is the length of the input list. This is because the function creates a new list to store the result tuples, which could be as large as the input list if all tuples meet the filter condition. The recursive call stack also uses O(n) space, as there could be n nested function calls in the worst case.
Method #4: Using operator.countOf() method
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #5 : Using filter() and lambda():
1.Define a list of tuples test_list.
2.Define the values of K and N.
3.Use the filter() function along with a lambda function to create a new list res of tuples that only contain tuples that have exactly N occurrences of the value K.
4.Convert the filter() object to a list using the list() function and assign it to the variable res.
5.Print out the filtered tuples as a string.
The original list is : [(4, 5, 6, 4, 4), (4, 4, 3), (4, 4, 4), (3, 4, 9)] Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity : O(n * m), where n is the length of the input list test_list and m is the maximum length of a tuple in the list. This is because the filter() function needs to iterate over every tuple in the list, and the count() method needs to iterate over every element in each tuple.
Auxiliary space : O(k), where k is the number of tuples that satisfy the filter condition. This is because the filter() function returns an iterator, and the list() function creates a new list to store the filtered tuples.
Method #6: Using for loop
Algorithm:
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time Complexity: O(n * k), where n is the number of tuples in the list and k is the average length of each tuple. This is because we are looping through each tuple in the list and then looping through each element in the tuple to count the occurrences of K.
Auxiliary Space: O(n), where n is the number of tuples in the list. This is because we are storing the filtered tuples in a list (res) which can contain at most n elements.
Method #7: Using filter() and partial() from functools module
Use the filter() method along with the partial() method from the functools module to filter the tuples based on the given conditions.
Step-by-step approach:
Below is the implementation of the above approach:
Filtered tuples : [(4, 5, 6, 4, 4), (4, 4, 4)]
Time complexity: O(N*M), where N is the number of tuples in the list and M is the maximum length of any tuple in the list.
Auxiliary space: O(1) - We are only creating a few variables and not using any additional data structures that depend on the size of the input.