Open In App

Python | Remove tuples from list of tuples if greater than n

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


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 

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


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 

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


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

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)


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:

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.


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.



Last Updated : 09 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads