Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Filter Range Length Tuples

Python – Filter Range Length Tuples

Sometimes, while working with records, we might desire to filter records in such a way in which we need to discard records that do not contains exact number of elements required to constitute a record and lie in a range. 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 range length required to constitute the record. The computation of length is done by len(). 

Python3




# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using list comprehension + len()
 
# Initializing list
test_list = [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing desired lengths
i, j = 2, 3
 
# Filter Range Length Tuples
# Using list comprehension + len()
res = [sub for sub in test_list if len(sub) >= i and len(sub) <= j]
     
# printing result
print("The tuple list after filtering range records : " + str(res))


Output : 

The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
The tuple list after filtering range records : [(5, 6), (2, 3, 5), (5, 9)]

Time Complexity: O(n*n) where n is the number of elements in the string list. The list comprehension + len() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.

  Method #2 : Using filter() + lambda + len() The combination of above functions can also be used to perform this particular task. In this, we just use filter() and use lambda function to filter range length records. 

Python3




# Python3 code to demonstrate working of
# Filter Range Length Tuples
# Using filter() + lambda + len()
 
# Initializing list
test_list = [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing desired lengths
i, j = 2, 3
 
# Filter Range Length Tuples
# Using filter() + lambda + len()
res = list(filter(lambda ele: len(ele) >= i and len(ele) <= j, test_list))
     
# printing result
print("The tuple list after filtering range records : " + str(res))


Output : 

The original list is : [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
The tuple list after filtering range records : [(5, 6), (2, 3, 5), (5, 9)]

Time Complexity: O(n*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”.

Using a for loop to filter tuples based on their length:

Approach:

  • Define the function filter_tuples that takes three parameters: tuples_list, min_length, and max_length.
  • Initialize an empty list called filtered_list.
  • Iterate over each tuple t in tuples_list.
  • Check if the length of t is between min_length and max_length (inclusive). If it is, append t to filtered_list.
  • Return filtered_list.

Python3




def filter_tuples(tuples_list, min_length, max_length):
    filtered_list = []
    for t in tuples_list:
        if min_length <= len(t) <= max_length:
            filtered_list.append(t)
    return filtered_list
 
original_list = [(4, ), (5, 6), (2, 3, 5), (5, 6, 8, 2), (5, 9)]
filtered_list = [t for t in original_list if 2 <= len(t) <= 3]
print(filtered_list)


Output

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

Time Complexity: O(n)
Auxiliary Space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments