Remove a list from a list of lists Python
You can use list comprehension. Here is a sample input and output. The idea is simple: For each sublist just check for the min
and max
if they fall outside the desired limits.
list_1 = [[0.0,3.3, 4.9, 7.5], [4, 6, 9, 11, 12.1], [3, 43, 99, 909, 2.11, 76, 76.9, 1000], ]
left = 3
right = 15
list_2 = [i for i in list_1 if (min(i)>=left and max(i)<=right)]
print (list_2)
# [[4, 6, 9, 11, 12.1]]
Your error comes from using the .pop()
method, which expects an integer index as its argument, when you really mean .remove()
. However, even after correcting this to .remove()
you may also experience errors from trying to remove items from a list while iterating over it. A cleaner approach is a list comprehension:
my_list = [[0.0,3.3, 4.9, 7.5], [4, 6, 90, 21, 21.1], [3, 43, 99, 909, 2.11, 76, 76.9, 1000]]
min_value = 3
max_value = 100
my_list[:] = [sublist for sublist in my_list if all(min_value <= x <= max_value for x in sublist)]