Python - Remove all elements less than 10 from a list
In Python, we can use a list comprehension to filter out all elements less than 10 from a list. A list comprehension is a concise way to create a new list by iterating over the original list and applying a condition to filter elements.
Example
original_list = [5, 12, 3, 18, 7, 20]
filtered_list = [x for x in original_list if x >= 10]
print(filtered_list)
filtered_list = [x for x in original_list if x >= 10]
print(filtered_list)
Code explanation:
original_listis the original list, containing some integers.filtered_listis the new list generated by the list comprehension.[x for x in original_list if x >= 10]The meaning of this line of code is: iterate overoriginal_listeach element inxifxis greater than or equal to 10, add it to the new listfiltered_listin.print(filtered_list)Used to output the filtered list.
Output result:
Example
[12, 18, 20]
Python3 Examples