Sometimes, while working with records, we might desire to filter records in such a way in we need to discard records that do not contain an exact number of elements required to constitute a record. Let's discuss certain ways in which this task can be performed.
Method #1: Using list comprehension + len()
In this method, we just iterate through the list and discard the tuples that do not match the matching length required to constitute the record. The computation of length is done by len().
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Using list comprehension + len()
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
# Using list comprehension + len()
res = [sub for sub in test_list if len(sub) == 3]
# printing result
print("The tuple list after removing uneven records: " + str(res))
Output : The original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time Complexity : O(n)
Space Complexity : O(n)
Method #2: Using filter() + lambda + len()
The combination of the above functions can also be used to perform this particular task. In this, we just use filter() and use lambda function to separate uneven-length records.
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Using filter() + lambda + len()
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
# Using filter() + lambda + len()
res = list(filter(lambda ele: len(ele) == N, test_list))
# printing result
print("The tuple list after removing uneven records: " + str(res))
Output : The original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time Complexity : O(n)
Space Complexity : O(m)
Method #3 : Using remove() and len() methods
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
for i in test_list:
if(len(i)!=3):
test_list.remove(i)
# printing result
print("The tuple list after removing uneven records: " + str(test_list))
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time Complexity : O(n^2)
Space Complexity : O(1)
Method#4: Using Recursive method.
Algorithm:
- Define a recursive function called filter_tuples_by_length that takes a tuple list and a desired length as input.
- The base case for the recursive function is an empty list, in which case the function should return an empty list.
- In the recursive case, we first check the length of the first tuple in the list. If the length matches the desired length, we add it to the result list and call the function recursively with the rest of the list. Otherwise, we just call the function recursively with the rest of the list.
- We call the filter_tuples_by_length function with the input tuple list and desired length.
- The filtered tuple list is returned as output.
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Using Recursive function
def filter_tuples_by_length(tuples, length):
# Base case: if the list of tuples is empty, return an empty list
if not tuples:
return []
# Recursive case: check the length of the first tuple, and if it matches the desired length,
# add it to the result list and call the function recursively with the rest of the list.
# Otherwise, just call the function recursively with the rest of the list.
if len(tuples[0]) == length:
return [tuples[0]] + filter_tuples_by_length(tuples[1:], length)
else:
return filter_tuples_by_length(tuples[1:], length)
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
res = filter_tuples_by_length(test_list,N)
# printing result
print("The tuple list after removing uneven records: " + str(res))
#this code contributed by tvsk
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time complexity:
The time complexity of the algorithm is O(n), where n is the length of the tuple list. This is because we traverse through the list of tuples once and check the length of each tuple.
Auxiliary space:
The space complexity of the algorithm is O(n), where n is the length of the tuple list. This is because we are using a recursive function to traverse through the list, and creating a new list to store the filtered tuples. Therefore, the maximum space required at any point in time is proportional to the length of the input list.
Method#5: Using list comprehension and a ternary operator:
Algorithm:
1.Initialize a list of tuples test_list.
2.Initialize the desired length of the tuples as N.
3.Iterate over each tuple in test_list and check if the length of the tuple is equal to N.
4.If the length of the tuple is not equal to N, remove it from the list using the remove() method.
5.Finally, print the updated test_list with only the tuples of length N.
Python3
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
test_list = [i if len(i) == N else None for i in test_list]
test_list = list(filter(lambda x: x is not None, test_list))
# printing result
print("The tuple list after removing uneven records: " + str(test_list))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time Complexity: The time complexity of this code is O(n^2), where n is the length of test_list. This is because, in the worst case, we might need to remove every tuple from the list, which would require iterating over the list n times in total (once for each element to check its length and once again for each element to remove it).
Auxiliary Space: The space complexity of this code is O(1) because we are not using any additional data structures or variables that are dependent on the size of test_list.
Method #6: Using a for loop and a conditional statement
Iterating through the list using a for loop and checking the length of each tuple using a conditional statement. If the length of the tuple is equal to the desired length N, then it is added to the result list res. Finally, the result list is printed.
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Using for loop and a conditional statement
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
# Using for loop and a conditional statement
res = []
for ele in test_list:
if len(ele) == N:
res.append(ele)
# printing result
print("The tuple list after removing uneven records: " + str(res))
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time Complexity: O(n^2), where n is the length of test_list.
Auxiliary Space: O(n^ 2)
Method #7: Using map() and filter()
Step-by-step approach:
- Initialize the input list test_list and the desired length N.
- Use the map() function to apply a lambda function to each tuple in test_list. The lambda function checks if the length of the tuple is equal to N. If it is, the original tuple is returned. Otherwise, None is returned.
- Convert the result of map() into a list.
- Use the filter() function to remove all elements from the list that are equal to None.
- Convert the result of filter() into a list.
- Print the final result.
Python3
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
N = 3
# printing original list
print("The original list is : " + str(test_list))
result = list(map(lambda tpl: tpl if len(tpl) == N else None, test_list))
result = list(filter(lambda tpl: tpl is not None, result))
# printing result
print("The tuple list after removing uneven records: " + str(result))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time complexity:
The code first iterates over the list test_list once to create a dictionary of element counts using a for loop. This takes O(n) time, where n is the length of test_list.
It then iterates over the keys and values in the dictionary using another for loop to find the most frequent element. This takes O(k) time, where k is the number of unique elements in test_list.
Therefore, the overall time complexity of the code is O(n + k).
Space complexity:
The code creates a dictionary to store element counts, which has a space complexity of O(k), where k is the number of unique elements in test_list.
Therefore, the overall space complexity of the code is O(k).
Method #8: Using a for loop and append()
- Initialize an empty list res.
- Initialize desired length N.
- Iterate over each tuple sub in test_list using a for loop.
- If the length of sub is equal to N, then append it to res.
- Print the res list.
Python3
# Python3 code to demonstrate working of
# Retain records of specific length
# Using a for loop and append()
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
# Using a for loop and append()
res = []
for sub in test_list:
if len(sub) == N:
res.append(sub)
# printing result
print("The tuple list after removing uneven records: " + str(res))
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time complexity: O(n), where n is the number of tuples in the test_list.
Auxiliary space: O(k), where k is the number of tuples in the test_list that have length equal to N.
Method 9: Using itertools.filterfalse() function
Import the itertools module.
Initialize a list named "test_list" containing tuples of different lengths.
Print the original list using the "print()" function.
Initialize a variable named "N" with the desired length of tuples.
Use the "itertools.filterfalse()" function to filter out tuples from "test_list" that do not have a length of "N".
Store the filtered list in a variable named "res".
Print the filtered list using the "print()" function.
Python3
import itertools
# Initializing list
test_list = [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing desired length
N = 3
# Retain records of specific length
# Using itertools.filterfalse() function
res = list(itertools.filterfalse(lambda sub: len(sub) != N, test_list))
# printing result
print("The tuple list after removing uneven records: " + str(res))
OutputThe original list is : [(4, 5, 6), (5, 6), (2, 3, 5), (5, 6, 8), (5, 9)]
The tuple list after removing uneven records: [(4, 5, 6), (2, 3, 5), (5, 6, 8)]
Time complexity: O(n), where n is the number of tuples in the test_list.
Auxiliary space: O(k), where k is the number of tuples in the test_list that have length equal to N.
Similar Reads
Python - Retain records with N occurrences of K 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
10 min read
Python | Retain K Front and Rear elements Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Letâs discuss certain ways in
8 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
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 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 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