Wednesday, October 9, 2024
Google search engine
HomeLanguagesPython | Remove particular element from tuple list

Python | Remove particular element from tuple list

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. 

Method  1: 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. 

  1. First, a list of tuples named test_list is initialized with some values.
  2. The original list is printed using the print() function and string concatenation.
  3. 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.
  4. 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.
  5. The final res list is printed using the print() function and string concatenation.

Python3




# Python3 code to demonstrate working of
# Remove particular element from tuple list
# using list comprehension
 
# initialize list
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# declaring remove element
N = 6
 
# Remove particular element from tuple list
# using list comprehension
res = [tuple(ele for ele in sub if ele != N) for sub in test_list]
 
# printing result
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.

Method 2 : Using remove(),list() and tuple() methods

Python3




# Python3 code to demonstrate working of
# Remove particular element from tuple list
 
# initialize list
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# declaring remove element
N = 6
 
# Remove particular element from tuple list
res = []
for i in test_list:
    i = list(i)
    while N in i:
        i.remove(N)
    res.append(tuple(i))
# printing result
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.

Method#3: Using Recursive method.

Algorithm:

  1. Define a function remove_element(lst, n) that takes a tuple list lst and an element n as input.
  2. If lst is empty, return an empty list.
  3. Otherwise, create a new tuple new_sub by iterating over the elements in the first tuple of lst and adding each element to
  4. new_sub if it is not equal to n.
  5. Return a list that contains new_sub followed by the result of recursively calling remove_element with the rest of lst and n.
  6. Initialize the original list test_list and the element to remove N.
  7. Call remove_element with test_list and N as arguments and store the result in res.
  8. Print the original list and the result list.

Python3




# Python3 code to demonstrate working of
# Remove particular element from tuple list
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)
 
# initialize list
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# declaring remove element
N = 6
 
# Remove particular element from tuple list
res = remove_element(test_list,N)
# printing result
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.

Method#4:Using enumeration

Step-by-step approach:

  • Initialize an empty list to hold the modified tuples
  • For each tuple in test_list:
    a. Initialize an empty tuple to hold the modified values
    b. For each element in the tuple:
    c. If the element is not equal to N, add it to the modified tuple
    d. Add the modified tuple to the list of modified tuples
  • Return the list of modified tuples

Below is the implementation of the above approach:
 

Python3




# initialize list
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# declaring remove element
N = 6
 
# Remove particular element from tuple list
for i, sub in enumerate(test_list):
    test_list[i] = tuple(ele for ele in sub if ele != N)
 
# printing result
print("The Tuple List after removal of element : " + str(test_list))
#This code is contributed by Vinay Pinjala.


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 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(nm), 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.

Method 5 : use the map() function in combination with lambda function.

Steps:

Initialize the tuple list, test_list, and print it.
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.
Convert the resulting map object to a list and print it.

Python3




# initialize list
test_list = [(5, 6, 7), (7, 2, 4, 6), (6, 6, 7), (6, 10, 8)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# declaring remove element
N = 6
 
# Remove particular element from tuple list
# using map() function and lambda function
res = list(map(lambda sub: tuple(filter(lambda ele: ele != N, sub)), test_list))
 
# printing result
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). 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments