# Using list comprehension
my_list = [10, 6, 11, 3, 8, 5, 14]
my_list = [x for x in my_list if x < 9]
print(my_list)
Output
[6, 3, 8, 5]
We are using list comprehension here to remove the elements that are less than a given value. We are removing thee items from a given Python list.
Code Example
numbers = [1, 5, 8, 9, 3, 7, 2]
numbers = [x for x in numbers if x < 4]
print(numbers)
Output
[1, 3, 2]
In the above code example:
We can also use the filter() function to remove items from a list that is less than a specific value. Below is the code example that will help you to understand it.
numbers = [1, 5, 8, 9, 3, 7, 2]
numbers = filter(lambda val: val < 4, numbers)
print(list(numbers))
Output
[1, 3, 2]
0 Comments