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 select elements.
Example
original_list
=
[
5
,
12
,
3
,
18
,
7
,
20
]
filtered_list
=
[
x
for
x
in
original_list
if
x
=
10
]
(
filtered_list
)
Code analysis:
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]This line means: iterate over every elementoriginal_listinx, ifxis greater than or equal to 10, add it to the new listfiltered_listit.print(filtered_list)Used to print the filtered list.
Output result:
Example
[
12
,
18
,
20
]
Python3 Examples