Saturday, November 23, 2024
Google search engine
HomeLanguagesPython – Remove particular data type Elements from Tuple

Python – Remove particular data type Elements from Tuple

Sometimes, while working with Python tuples, we can have a problem in which we need to remove particular data type elements from tuple. This kind of problem can occur in domains which require data preprocessing. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (4, 5, ‘Gfg’, 7.7, ‘Best’), data_type = str 
Output : [4, 5, 7.7]

 Input : test_tuple = (4, 5, ‘Gfg’, 7.7, ‘Best’), data_type = float 
Output : [4, 5, ‘Gfg’, ‘Best’]

Method #1 : Using loop + isinstance() The combination of above functionalities can be used to solve this problem. In this, we need to iterate for each element and discard the element if it matches the data type, using isinstance(). 

Python3




# Python3 code to demonstrate working of
# Remove particular data type Elements from Tuple
# Using loop + isinstance()
 
# initializing tuple
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = int
 
# Remove particular data type Elements from Tuple
# Using loop + isinstance()
res = []
for ele in test_tuple:
    if not isinstance(ele, data_type):
        res.append(ele)
 
# printing result
print("The filtered tuple : " + str(res))


Output : 

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

  Method #2 : Using list comprehension + isinstance() This is yet another way in which this task can be performed. In this, we need to perform similar task using a shorthand by list comprehension. 

Python3




# Python3 code to demonstrate working of
# Remove particular data type Elements from Tuple
# Using list comprehension + isinstance()
 
# initializing tuple
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = int
 
# Remove particular data type Elements from Tuple
# Using list comprehension + isinstance()
res = [ele for ele in test_tuple if not isinstance(ele, data_type)]
 
# printing result
print("The filtered tuple : " + str(res))


Output : 

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

Method #3: Using type() method.type() method returns the data type of variable.Check the type of variable in tuple, if it doesn’t matches the given type append them to the output list and display the list

Python3




# Python3 code to demonstrate working of
# Remove particular data type Elements from Tuple
 
# initializing tuple
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = int
 
# Remove particular data type Elements from Tuple
res=[]
for i in test_tuple:
    if(not (type(i) is data_type)):
        res.append(i)
# printing result
print("The filtered tuple : " + str(res))


Output

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

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

The space complexity of this code is also O(n), because we are creating a new list, res, that can potentially store up to n elements, where n is the length of the input tuple.

Method #4: Using filter()

Python3




# initializing tuple
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = int
 
# Remove particular data type Elements from Tuple using filter
res = list(filter(lambda ele: not isinstance(ele, data_type), test_tuple))
 
# printing result
print("The filtered tuple : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

Time complexity: O(n)

Space complexity : O(n)

Method#5: Using Recursive method.

Algorithm:

  1. If the input tuple is empty, return an empty tuple.
  2. Otherwise, split the tuple into the head (first element) and tail (remaining elements).
  3. If the head element is of the specified data type, recursively call the function on the tail of the tuple.
  4. Otherwise, include the head element in the result tuple, and recursively call the function on the tail of the tuple.
  5. Repeat steps 2-4 until the entire tuple has been processed.
  6. Return the resulting tuple with all elements of the specified data type removed.

Python3




def remove_data_type(tup, data_type):
    if not tup:
        return ()
 
    head, *tail = tup
 
    if isinstance(head, data_type):
        return remove_data_type(tail, data_type)
 
    return (head,) + remove_data_type(tail, data_type)
 
# initializing tuple
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = int
 
# Remove particular data type Elements from Tuple using Recursive method
res = list(remove_data_type(test_tuple, data_type))
 
# printing result
print("The filtered tuple : " + str(res))
#This code is contributed by Tvsk.


Output

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

The time complexity of this function is O(n), where n is the length of the input tuple. This is because the function recursively processes each element of the tuple exactly once, resulting in a linear time complexity.

The space complexity of this function is also O(n), due to the recursive calls that are made on the tail of the tuple. Each recursive call creates a new tuple object that contains a subset of the original tuple, resulting in a space complexity that scales linearly with the size of the input tuple. However, note that the final output tuple is not created until all recursive calls have returned, so the space complexity of the function does not include the space required for the final output tuple.

Method#6:Using reduce() and lambda function: 

Algorithm : 

  • Import the reduce() function from the functools module.
  • Define the input tuple and the data type to be removed.
  • Use the reduce() function to iterate through the input tuple and create a new tuple containing only the elements that are not of the specified data type.
  • Convert the resulting tuple to a list.
  • Print the final list.

Python3




from functools import reduce
 
test_tuple = (4, 5, 'Gfg', 7.7, 'Best')
data_type = int
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
res = reduce(lambda x, y: x + (y,) if not isinstance(y, data_type) else x, test_tuple, ())
res = list(res)
# printing result
print("The filtered tuple : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original tuple : (4, 5, 'Gfg', 7.7, 'Best')
The filtered tuple : ['Gfg', 7.7, 'Best']

The time complexity: O(n), where n is the length of the input tuple. This is because the reduce() function iterates through each element in the tuple exactly once.
The Auxiliary space: O(n), because a new tuple is created to hold the filtered elements, and then this tuple is converted to a list.

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