
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Remove list elements larger than a specific value using Python
In this article, we will learn how to remove elements larger than a specific value from a list in Python.
Methods Used
The following are the various methods used to accomplish this task −
Using remove() Method
Using List Comprehension
Using the filter() method & lambda function
Method 1: Using remove() Method
remove() function(removes the first occurrence of the element from the list)
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Create a variable to store the input list.
Create another variable to store another input value.
Use the for loop to iterate through each element in an input list.
Use the if conditional statement, to check whether the current element is greater than the specified input value.
Use the to remove() function to remove that current element from the list if the condition is true by passing it as an argument to it.
Print the resultant list after removing elements larger than the specified input value.
Example
The following program removes elements larger than the specified input value from the list using the remove() function −
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] # Printing the given list print("The Given list is:", inputList) # input value inputValue = 50 # iterarting through the list for i in inputList: # checking whether the current element is greater than the input value if i > inputValue: # removing that current element from the list if the condition is true inputList.remove(i) # printing the resultant list after removing elements larger than 50 print("Removing elements larger than 50 from the list:\n", inputList)
Output
On execution, the above program will generate the following output −
The Given list is: [45, 150, 20, 90, 15, 55, 12, 75] Removing elements larger than 50 from the list: [45, 20, 15, 12]
Method 2: Using List Comprehension
List Comprehension
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Example
The following program removes elements larger than the specified input value from the input list using list comprehension −
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] # Printing the given list print("The Given list is:", inputList) # input value inputValue = 50 # removing elements from a list larger than 50 # by traversing through the list and storing elements # that are having a value less than or equal to the given input value resultList = [k for k in inputList if k <= inputValue] # printing the resultant list print("Removing elements larger than 50 from the list:", resultList)
Output
On execution, the above program will generate the following output −
The Given list is: [45, 150, 20, 90, 15, 55, 12, 75] Removing elements larger than 50 from the list: [45, 20, 15, 12]
Method 3: Using the filter() method & lambda function
Lambda Function
Lambda Function, often known as an 'Anonymous Function,' is the same as a normal Python function except that it can be defined without a name. The def keyword is used to define normal functions, while the lambda keyword is used to define anonymous functions. They are, however, limited to a single line of expression. They, like regular functions, can accept several parameters.
Syntax
lambda arguments: expression
This function accepts any number of inputs but only evaluates and returns one expression.
Lambda functions can be used wherever function objects are necessary.
You must remember that lambda functions are syntactically limited to a single expression.
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task. −
Check each element of the iterable using the lambda function.
Filter all the elements that are having a value less than the given input value using the filter() function.
filter() function − filters the specified sequence using a function that determines if each element in the sequence is true or false.
Convert this filter object to a list using the list() function.
Print the resultant list after removing elements larger than the specified input value.
Example
The following program removes elements larger than the specified input value from the input list using filter() and lambda() functions &miinus;
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] print("The Given list is:", inputList) # input value inputValue = 50 # Filtering list objects that are having value # less than or equal to the given input Value filteredObject = filter(lambda k: k <= inputValue, inputList) # Convert the filter object to a list using the list() function resultList = list(filteredObject) # printing the resultant list after removing elements larger than 50 print("Removing elements larger than 50 from the list:\n", resultList)
Output
On execution, the above program will generate the following output −
The Given list is: [45, 150, 20, 90, 15, 55, 12, 75] Removing elements larger than 50 from the list: [45, 20, 15, 12]
Method 4: Using the for loop and append() function
Example
The following program removes elements larger than the specified input value from the input list using for loop and append() function −
# input list inputList = [45, 150, 20, 90, 15, 55, 12, 75] print("The Given list is:", inputList) # input value inputValue = 50 # Creating an empty list to store the result resultList = [] # iterarting through the list for i in inputList: # checking whether the current element is less than or equal to the input value if i <= inputValue: # add this element to the result list resultList.append(i) # printing the resultant list after removing elements larger than 50 print("Removing elements larger than 50 from the list:\n", resultList)
Output
On execution, the above program will generate the following output −
The Given list is: [45, 150, 20, 90, 15, 55, 12, 75] Removing elements larger than 50 from the list: [45, 20, 15, 12]
Conclusion
In this article, we learned 4 different Python methods for removing list elements that are larger than a given value. Additionally, we learned how to use the lambda and filter() functions to filter the list based on the condition.
- Related Articles
- Python – Split List on next larger value
- Python Program to remove elements that are less than K difference away in a list
- Remove elements from a linked list using Javascript
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Python program to remove null value from a list
- Python – Check if elements in a specific index are equal for list elements
- Python program to remove duplicate elements from a Circular Linked List
- Python Program to remove a specific digit from every element of the list
- Python program to remove elements at Indices in List
- Find the list elements starting with specific letter in Python
- How to get values greater than a specific value from an embedded list in MongoDB?
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python - Assign a specific value to Non Max-Min elements in Tuple
- Python prorgam to remove duplicate elements index from other list
