Open In App

Python | Remove particular element from tuple list

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Since the advent of the popularity of Python in data analysis, we have a list of tuples as a container in many of our problems. Sometimes, while data preprocessing, we might have a problem in which we need to completely remove a particular element from a list of tuples. Let’s discuss a way in which this task can be performed. 

Removing element for tuple list in Python

Remove an item from a tuple using list comprehension 

This task can be used using a brute force manner using a loop, but a better alternative shorthand would be an approach that could perform this task in one line. List comprehension can help us achieve it and hence it’s recommended to use this method to perform this task. This just checks for an element and removes if it’s the selected element. 

First, a list of tuples named test_list is initialized with some values.The original list is printed using the print() function and string concatenation.A variable N is declared and assigned a value of 6. This value represents the element that needs to be removed from each tuple in the list.A list comprehension is used to remove the specified element from each tuple in the list. The comprehension iterates through each tuple in test_list and through each element in the tuple. It checks if the element is equal to N, and if it is not, it adds the element to a new tuple. This new tuple is added to a list named res.The final res list is printed using the print() function and string concatenation.

Python3




test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
print("The original list is : " + str(test_list))
N = 6
res = [tuple(ele for ele in sub if ele != N) for sub in test_list]
print("The Tuple List after removal of element : " + str(res))


Output

The original list is : [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
The Tuple List after removal of element : [(5, 7), (7, 2, 4), (7,), (10, 8)]

Time Complexity: O(n * m), where n is the number of tuples in the list and m is the number of elements in each tuple.
Auxiliary Space: O(n * m), where n is the number of tuples in the list and m is the number of elements in each tuple.

Python Remove Tuple Items using remove(),list() and tuple() Method

The code removes the element N from tuples in the list test_list. It iterates through each tuple, removes all occurrences of N, and stores the modified tuples in a new list called res.

Python3




test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
print("The original list is : " + str(test_list))
N = 6
res = []
for i in test_list:
    i = list(i)
    while N in i:
        i.remove(N)
    res.append(tuple(i))
print("The Tuple List after removal of element : " + str(res))


Output

The original list is : [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
The Tuple List after removal of element : [(5, 7), (7, 2, 4), (7,), (10, 8)]

Time complexity: O(n^2) where n is the number of tuples in the list. 
Auxiliary space: O(n) as we are creating a new list to store the tuples after removing the element.

Remove an item from a tuple using Recursive Method

Here first we define a function remove_element(lst, n) that takes a tuple list lst and an element n as input.If lst is empty, return an empty list.Otherwise, create a new tuple new_sub by iterating over the elements in the first tuple of lst and adding each element to new_sub if it is not equal to n.Then we return a list that contains new_sub followed by the result of recursively calling remove_element with the rest of lst and n. Then we print the original list and the result list.

Python3




def remove_element(lst, n):
    if not lst:
        return []
    new_sub = tuple(ele for ele in lst[0] if ele != n)
    return [new_sub] + remove_element(lst[1:], n)
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
print("The original list is : " + str(test_list))
N = 6
res = remove_element(test_list,N)
print("The Tuple List after removal of element : " + str(res))


Output

The original list is : [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
The Tuple List after removal of element : [(5, 7), (7, 2, 4), (7,), (10, 8)]

Time complexity: O(nm) ,where n is the number of tuples in the list and m is the maximum number of elements in a tuple.
Auxiliary Space: O(nm), for the output list. The recursive function uses O(n) stack space.

Python Remove Tuple Items using enumeration

First we Initialize an empty list to hold the modified tuples . For each tuple in test_list then
Initialize an empty tuple to hold the modified values.Then for each element in the tuple If the element is not equal to N, add it to the modified tuple and add the modified tuple to the list of modified tuples. Then return the list of modified tuples and then below is the implementation of the above approach:
 

Python3




test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
print("The original list is : " + str(test_list))
N = 6
for i, sub in enumerate(test_list):
    test_list[i] = tuple(ele for ele in sub if ele != N)
print("The Tuple List after removal of element : " + str(test_list))


Output

The original list is : [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
The Tuple List after removal of element : [(5, 7), (7, 2, 4), (7,), (10, 8)]

Time complexity: O(n*m), where n is the number of tuples in test_list and m is the maximum number of elements in a single tuple. The algorithm iterates over each element in each tuple.
Auxiliary Space: O(n*m), where n is the number of tuples in test_list and m is the maximum number of elements in a single tuple. The algorithm creates a new list of modified tuples, and a new tuple for each modified tuple, so the space required is proportional to the input size.

Using the map() function in combination with lambda function

First we Initialize the tuple list, test_list, and print it and then declare the element to be removed, N.
Use the map() function and a lambda function to iterate over each tuple in the test_list and return a new tuple after filtering out the element N from it. The filter() function is used inside the lambda function to filter out N from each tuple. At last convert the resulting map object to a list and print it.

Python3




test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
print("The original list is : " + str(test_list))
N = 6
res = list(map(lambda sub: tuple(filter(lambda ele: ele != N, sub)), test_list))
print("The Tuple List after removal of element : " + str(res))


Output

The original list is : [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
The Tuple List after removal of element : [(5, 7), (7, 2, 4), (7,), (10, 8)]

The time complexity of this code is O(n^2), where n is the length of the input list.

The space complexity is also O(n^2), because we are creating a new list of tuples in res, where each tuple can have up to n elements (the length of the input tuples). 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads