Python | Remove tuples from list of tuples if greater than n
Last Updated :
09 Apr, 2023
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
STEPS:
- Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]
- Print the initial list: print("intial_list", str(ini_tuple))
- Define the condition that determines which tuples should be removed from the list. In this case, we want to remove tuples where the second element (index 1) is greater than 100.
- Use a list comprehension to iterate over each tuple in ini_tuple and create a new list containing only those tuples that satisfy the condition: result = [i for i in ini_tuple if i[1] <= 100]
- Print the resultant tuple list: print("Resultant tuple list: ", str(result))
Python3
# 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 initial_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))
Outputintial_list [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]
Resultant tuple list: [('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n), where n is the number of tuples in the initial list.
Auxiliary space: O(m), where m is the number of tuples that satisfy the condition.
Method #2: Using filter + lambda
Python3
# 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))
Outputintial_list [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]
Resultant tuple list: [('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n), where n is the number of tuples in tuple.
Auxiliary space: O(m), where m is the number of tuples in result.
Method #3: Using Naive Method
Python3
# 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))
Outputintial_list [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]
Resultant tuple list: [('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n), where n is the number of tuples in the ini_tuple list.
Auxiliary space: O(m), where m is the number of tuples in the result list
Method #4: Using list comprehension
Python3
tup = [('b', 100), ('c', 200), ('c', 45),('d', 876), ('e', 75)]
print([i for i in tup if i[1] <= 100])
Output[('b', 100), ('c', 45), ('e', 75)]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method: Using enumerate function
Python3
tup = [('b', 100), ('c', 200), ('c', 45),('d', 876), ('e', 75)]
print([i for a,i in enumerate(tup) if i[1] <= 100])
Output[('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n), where n is the length of the input list tup.
Auxiliary space: O(n), because the list comprehension creates a new list to store the filtered tuples.
Alternative method:
Python3
tup = [('b', 100), ('c', 200), ('c', 45),('d', 876), ('e', 75)]
x=[i[1] for i in tup]
x=list(filter(lambda i:(i<=100),x))
print(x)
Time complexity: O(n), where n is the length of the input list 'tup',
Auxiliary space: O(m), where m is the number of elements in the resulting list 'x'.
Alternative method:
One additional approach to remove tuples from a list of tuples if their second element is greater than a certain value n is to use the remove method of the list object. This method removes the first occurrence of the specified value in the list.
For example:
Python3
# Initialize list of tuples
tup = [('b', 100), ('c', 200), ('c', 45),('d', 876), ('e', 75)]
# Remove tuples with second element greater than 100
for t in tup:
if t[1] > 100:
tup.remove(t)
print(tup) # Output: [('b', 100), ('c', 45), ('e', 75)]
#This code is contributed by Edula Vinay Kumar Reddy
Output[('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n^2) as the remove method takes O(n) time to remove an element from the list and the loop iterates through the list n times.
Auxiliary Space: O(1) as it does not create any additional data structures.
Method: Using a recursive function
Python3
#defining recursive function to remove tuple having greater than value list
def remove_tuple(start,oldlist,n,newlist):
if start==len(oldlist): #base condition
return newlist
if oldlist[start][1]>n: #checking the value greater than n
pass
else:
newlist.append(oldlist[start]) #appending tuple to newlist
return remove_tuple(start+1,oldlist,n,newlist) #recursive function call
# Initialize list of tuples
tup = [('b', 100), ('c', 200), ('c', 45),('d', 876), ('e', 75)]
n=100 #the value need to remove that contains in tuple
res=remove_tuple(0,tup,n,[])
print(res)
# Output: [('b', 100), ('c', 45), ('e', 75)]
#This code is contributed by tvsk
Output[('b', 100), ('c', 45), ('e', 75)]
Time complexity: O(n)
Auxiliary Space: O(n).
Method: Using itertools.filterfalse function:
Algorithm:
Define the initial tuple.
Define the condition to remove tuples (second element <= 100).
Use itertools.filterfalse to get the tuples that meet the condition.
Use filter to remove the tuples in the filtered list from the initial tuple.
Print the result.
Python3
import itertools
# initialising _list
ini_tuple = [('b', 100), ('c', 200), ('c', 45),
('d', 876), ('e', 75)]
# printing iniial_tuplelist
print("intial_list", str(ini_tuple))
result = list(filter(lambda x: x not in list(itertools.filterfalse(lambda y: y[1] <= 100, ini_tuple)), ini_tuple))
# printing resultant tuple list
print("Resultant tuple list: ", str(result))
#This code is contributed by Jyothi Pinjala.
Outputintial_list [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]
Resultant tuple list: [('b', 100), ('c', 45), ('e', 75)]
Time Complexity:
The itertools.filterfalse function in step 3 has a time complexity of O(N) where N is the number of tuples in the initial list.
The filter function in step 4 has a time complexity of O(N) where N is the number of tuples in the initial list.
Therefore, the overall time complexity of the code is O(N).
Auxiliary Space:
The space complexity of the code is O(N) where N is the number of tuples in the initial list. This is because we are creating a new list for the filtered tuples and another list for the result.
Similar Reads
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Generating a "Set Of Tuples" from A "List of Tuples" - Python
We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
3 min read
Python | Reverse each tuple in a list of tuples
Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega
5 min read
Python - Remove Tuples with difference greater than K
Given Dual Tuples List, remove pairs with differences greater than K. Input : test_list = [(4, 8), (1, 7), (9, 12), (3, 12), (2, 10)], K = 6 Output : [(4, 8), (9, 12), (1, 7)] Explanation : 4 (8 - 4), 3 (12 - 9) and 6 are all not greater than 6, hence retained. Input : test_list = [(4, 8), (1, 7), (
5 min read
Python | Remove tuple from list of tuples if not containing any character
Given a list of tuples, the task is to remove all those tuples which do not contain any character value. Example: Input: [(', ', 12), ('...', 55), ('-Geek', 115), ('Geeksfor', 115),] Output: [('-Geek', 115), ('Geeksfor', 115)] Method #1 : Using list comprehension Python3 # Python code to remove all
4 min read
Python | Remove all strings from a list of tuples
Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
8 min read
Flatten tuple of List to tuple - Python
The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Swap tuple elements in list of tuples - Python
The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
3 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), (
7 min read
Python - Test if greater than preceding element in Tuple List
Given list of tuples, check if preceding element is smaller than the current element for each element in Tuple list. Input : test_list = [(5, 1), (4, 9), (3, 5)] Output : [[False, False], [False, True], [False, True]] Explanation : First element always being False, Next element is checked for greate
9 min read