Open In App

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

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
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.



Similar Reads

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), (9, 12), (3, 12), (2, 10)], K = 3 Output : [(9, 12)
5 min read
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')] Method #1 : List comprehension This is a naive
5 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), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python - Filter Tuples Product greater than K
Given a Tuple list, extract all with product greater than K. Input : test_list = [(4, 5, 7), (1, 2, 3), (8, 4, 2), (2, 3, 4)], K = 50 Output : [(4, 5, 7), (8, 4, 2)] Explanation : 140 and 64 are greater than 50, hence tuples extracted.Input : test_list = [(4, 5, 7), (1, 2, 3), (8, 4, 2), (2, 3, 4)], K = 100 Output : [(4, 5, 7)] Explanation : 140 is
7 min read
Python - Find all pairs of consecutive odd positive integer smaller than a with sum greater than b
Given two positive integers a and b, the task is to write a program in python to find all pairs of consecutive odd numbers which are smaller than the first number a and their sum should be greater than the second number b. Examples: Input: a = 60 b = 100 Output: Pairs of consecutive number are: 51 , 53 53 , 55 55 , 57 57 , 59 Input: a = 20 b = 200
3 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read
Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this task can be performed. Method 1 (Using eval() + list
4 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Practice Tags :