Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it’s greater than n (say 100). Let’s discuss a few methods for the same.
Method #1: Using lambda
# Python code to demonstrate # to remove the tuples # if certain criteria met # initialising _list ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)] # printing iniial_tuplelist print("intial_list", str(ini_tuple)) # removing tuples for condition met result = [i for i in ini_tuple if i[1] <= 100] # printing resultant tuple list print ("Resultant tuple list: ", str(result)) |
Output:
intial_list [(‘b’, 100), (‘c’, 200), (‘c’, 45), (‘d’, 876), (‘e’, 75)]
Resultant tuple list: [(‘b’, 100), (‘c’, 45), (‘e’, 75)]
Method #2: Using filter + lambda
# Python code to demonstrate # to remove the tuples # if certain criteria met # initialising _list ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)] # printing iniial_tuplelist print("intial_list", str(ini_tuple)) # removing tuples for condition met result = list(filter(lambda x: x[1] <= 100, ini_tuple)) # printing resultant tuple list print ("Resultant tuple list: ", str(result)) |
Output:
intial_list [(‘b’, 100), (‘c’, 200), (‘c’, 45), (‘d’, 876), (‘e’, 75)]
Resultant tuple list: [(‘b’, 100), (‘c’, 45), (‘e’, 75)]
Method #3: Using Naive Method
# Python code to demonstrate # to remove the tuples # if certain criteria met # initialising _list ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)] # printing iniial_tuplelist print("intial_list", str(ini_tuple)) # removing tuples for condition met result = [] for i in ini_tuple: if i[1] <= 100: result.append(i) # printing resultant tuple list print ("Resultant tuple list: ", str(result)) |
Output:
intial_list [(‘b’, 100), (‘c’, 200), (‘c’, 45), (‘d’, 876), (‘e’, 75)]
Resultant tuple list: [(‘b’, 100), (‘c’, 45), (‘e’, 75)]
Recommended Posts:
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Remove empty tuples from a list
- Python | Remove all strings from a list of tuples
- Python | Remove tuple from list of tuples if not containing any character
- Python | Remove matching tuples
- Python | Min and Max value in list of tuples
- Python | Group tuples in list with same first value
- Python | Unzip a list of tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Selective value selection in list of tuples
- Python | Convert dictionary to list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



