Open In App

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 



STEPS:




# 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))

Output

intial_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 




# 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)]

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 




# 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)]

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




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 




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: 




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)

Output
[100, 45, 75]

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:




# 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




#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.




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.

Output
intial_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.


Article Tags :