Remove all sublists outside a given range using Python

In Python, you can remove sublists that fall outside a given range using several approaches. This article explores three effective methods: list comprehension, iterative removal, and filtering with append. Each method offers different advantages depending on your specific needs.

Using List Comprehension

List comprehension provides a concise way to filter sublists based on range conditions. This method creates a new list containing only sublists where all elements fall within the specified range.

Algorithm

  • Step 1 ? Define a function that takes the main list and range bounds as parameters

  • Step 2 ? Use list comprehension to check if both minimum and maximum values of each sublist are within range

  • Step 3 ? Return the filtered list containing only valid sublists

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("Filtered sublists:", result)
Filtered sublists: [[4, 5, 6], [7, 8, 9]]

Using Iterative Removal

This approach modifies the original list in-place by removing sublists that don't meet the range criteria. It uses a while loop to safely iterate while removing elements.

Algorithm

  • Step 1 ? Initialize an index counter starting from 0

  • Step 2 ? Use a while loop to iterate through the list

  • Step 3 ? Check if sublist elements are outside the range and remove if necessary

  • Step 4 ? Increment index only when keeping a sublist

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.copy(), lower_bound, upper_bound)
print("Modified list:", result)
Modified list: [[4, 5, 6], [7, 8, 9]]

Using Filtering and Append

This method creates a new list by iterating through sublists and appending only those that meet the range criteria. It offers explicit control over the filtering process.

Algorithm

  • Step 1 ? Create an empty list to store filtered results

  • Step 2 ? Iterate through each sublist in the original list

  • Step 3 ? Check if all elements in the sublist fall within the range

  • Step 4 ? Append valid sublists to the filtered list

Example

def remove_sublists_range(lst, lower, upper):
    filtered_lst = []
    for sublist in lst:
        if min(sublist) >= lower and max(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("Filtered result:", result)
Filtered result: [[4, 5, 6], [7, 8, 9]]

Comparison

Method Modifies Original Memory Usage Best For
List Comprehension No Creates new list Concise, readable code
Iterative Removal Yes Memory efficient In-place modification
Filtering and Append No Creates new list Explicit control

Conclusion

List comprehension offers the most concise syntax for filtering sublists outside a range. Use iterative removal when you need to modify the original list in-place. The append method provides explicit control and is easiest to understand for beginners.

Updated on: 2026-03-27T14:24:35+05:30

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements