Open In App

Python | Retain records of specific length

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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))


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^2)
Space Complexity : O(1)

Method#4: Using Recursive method.

Algorithm:

  1. Define a recursive function called filter_tuples_by_length that takes a tuple list and a desired length as input.
  2. The base case for the recursive function is an empty list, in which case the function should return an empty list.
  3. 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.
  4. We call the filter_tuples_by_length function with the input tuple list and desired length.
  5. 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


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:

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.


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: 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))


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^2), where n is the length of test_list. 
Auxiliary Space: O(n^ 2) 

Method #7: Using map() and filter()

Step-by-step approach:

  1. Initialize the input list test_list and the desired length N.
  2. 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.
  3. Convert the result of map() into a list.
  4. Use the filter() function to remove all elements from the list that are equal to None.
  5. Convert the result of filter() into a list.
  6. 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.


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:

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))


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), 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))


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), 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.



Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads