Remove elements larger than a specific value from a list in Python
In this post, I’ll explain to you how you can remove elements larger than a specific value from a list in Python.
Removing elements larger than a specific value from a list
To perform this task we can do it using for loop.
We can iterate through the list and then we’ll check the list elements with a value let’s suppose we have to remove the values larger than 5 then we’ll check each value of the list if the value is greater than 5 we’ll remove it otherwise we’ll not remove it.
After iterating through the whole list and removing all elements greater than a value we’ll get the result.
# Python program to remove elements from the list whose values is greater than 5 or you can use any other value in place of 5 list = [1, 3, 4, 7, 8, 13, 45] # now using loop x = [] #empty list to store values less than 5 for i in list: if i <= 5: x.append(i) #append values in new list x print(x) #print the list which contains values less than 5
Output:-
[1, 3, 4]
You can also read: Get Unique Values from a Python List
Leave a Reply