Remove all sublists outside a given range using Python


In Python, there are various ways to clear sub lists that drop outside an indicated run. The user will finish this assignment viably by leveraging list comprehension, iterative removal, or filtering and reproducing. List comprehension gives a brief approach by making an unused list based on given conditions. Iterative departure alters the introductory list by emptying sub lists that do not meet the run criteria. Filtering and duplicating strike an alter between memory viability and lucidness. In this article, the user will understand how to remove all sublists outside a given range using Python.

Approach 1: Removing all Sublists outside a given range using List comprehension method

List comprehension could be a brief and exquisite way to manipulate lists in Python. It permits us to form unused records based on existing ones by applying certain conditions. To remove sublists exterior a given extend, able to use list comprehension at the side suitable conditions. Let's jump into the calculation and execution steps −

Algorithm

  • Step 1 − Define the specified function remove_sublists_range() that contains three arguments in the function definition.

  • Step 2 − Check if the minimum value of the sublist is within the given range. If it is, keep the sublist; otherwise, exclude it.

  • Step 3 − Store the filtered sublists in a new list.

  • Step 4 − Print the resulting list.

Example

def remove_sublists_range(lst, lower, upper):
    filtered_lst = [sublist for sublist in lst if min(sublist) >= lower and max(sublist) <= upper]
    return filtered_lst

# Example usage
main_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
lower_bound = 4
upper_bound = 10
result = remove_sublists_range(main_list, lower_bound, upper_bound)
print(result)

Output

[[4, 5, 6], [7, 8, 9]]

Approach 2: Removing all Sublists outside a given range using Iterative Removal Method

Algorithm

In this approach, we'll emphasize the most list and remove the sublists that drop outside the given run. This will be accomplished by adjusting the initial list in-place. Let's go through the calculation and usage steps −

  • Step 1 − Define the function named remove_sublists_range().

  • Step 2 − Use a while loop for iteration over each sublist.

  • Step 3 − Check if the sublist’s minimum value is within the given range. If it is, keep the sublist; otherwise, eliminate it from the main list.

  • Step 4 − Print the resulting list.

Example

def remove_sublists_range(lst, lower, upper):
    i = 0
    while i < len(lst):
        sublist = lst[i]
        if min(sublist) < lower or max(sublist) > upper:
            lst.pop(i)
        else:
            i += 1
    return lst

# Example usage
main_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
lower_bound = 4
upper_bound = 10
result = remove_sublists_range(main_list, lower_bound, upper_bound)
print(result)

Output

[[4, 5, 6], [7, 8, 9]]

Approach 3: Removing all Sublists outside a given range using Append Method

This approach combines the preferences of past approaches. It makes an unused list by sifting the initial list based on the given run, comparative to the list comprehension approach, but without making an unused list for each cycle. Let's investigate the calculation and usage steps:

Algorithm

  • Step 1 − Characterize the given run, which indicates the lower and upper bounds.

  • Step 2 − Make a list to store the sifted sublists

  • Step 3 − Iterate each sublist within the primary list.

  • Step 4 − Check in the condition that the min(sublist) is inside the given extend. On the off chance that it is, add the sublist to the filtered_lst.

  • Step 5 − Print the coming about the list. 

Example

def remove_sublists_range(lst, lower, upper):
    filtered_lst = []
    for sublist in lst:
        if lower <= min(sublist) < upper:
            filtered_lst.append(sublist)
    return filtered_lst

# Example usage
main_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
lower_bound = 4
upper_bound = 10
result = remove_sublists_range(main_list, lower_bound, upper_bound)
print(result)

Output

[[4, 5, 6], [7, 8, 9]]

Conclusion

In this article, we've demonstrated three different approaches to eliminate sublists that are out of a given scope using Python. The first approach utilized a list comprehension to create a new list that satisfies the specified range constraint. The second approach involves iterative deletion, which directly modifies the original list by deleting out-of-bounds sublists. List comprehension is sophisticated, but new lists are created, iterative deletions directly modify lists and filtering and copying balance storage efficiency and readability. 

Updated on: 04-Sep-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements