Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Removing strings from tuple

Python | Removing strings from tuple

Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let’s discuss certain ways in which this particular task can be achieved. 

Method #1 : Using list comprehension + type() The combination of above 2 functions can be used to solve this particular problem. The list comprehension does the task of reconstruction of the modified list and type function helps us to filter the strings. 

Python3




# Python3 code to demonstrate
# Remove string from tuples
# using list comprehension + type()
 
# initializing list
test_list = [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + type()
# Remove string from tuples
res = [tuple([j for j in i if type(j) != str])
                           for i in test_list]
 
# print result
print("The list after string removal is : " + str(res))


Output : 

The original list : [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
The list after string removal is : [(1, 2), (4, ), (45, )]

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the list comprehension + type() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list

  Method #2 : Using list comprehension + isinstance() This is almost a similar method to perform this particular task but the change here is just to use the isinstance function to check for the string data type, and rest of the formulations remains mostly similar. 

Python3




# Python3 code to demonstrate
# Remove string from tuples
# using list comprehension + isinstance()
 
# initializing list
test_list = [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + isinstance()
# Remove string from tuples
res = [tuple(j for j in i if not isinstance(j, str))
                                 for i in test_list]
 
# print result
print("The list after string removal is : " + str(res))


Output : 

The original list : [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
The list after string removal is : [(1, 2), (4, ), (45, )]

Method #3 : Using filter() and tuple():

In this approach, we use the filter() function to remove strings from the tuples in the list and then convert the resulting filter object to a tuple using the tuple() function.

The filter() function takes in a function and an iterable as arguments and returns an iterator that only contains elements from the iterable for which the function returns True. In this case, the function is a lambda function that takes in a variable x and returns True if x is not a string, and False otherwise.

The tuple() function takes in an iterable as an argument and returns a tuple containing the elements of the iterable.

Here is the code snippet:
 

Python3




# Python3 code to demonstrate
# Remove string from tuples
 
# Initialize the list of tuples
test_list = [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
# printing original list
print("The original list : " + str(test_list))
 
# Use filter() to remove strings from the tuples and then convert the resulting
# filter object to a tuple using tuple()
res = [tuple(filter(lambda x: not isinstance(x, str), tup)) for tup in test_list]
 
# print result
print("The list after string removal is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [('Geeks', 1, 2), ('for', 4, 'Geeks'), (45, 'good')]
The list after string removal is : [(1, 2), (4,), (45,)]

Time complexity: O(n)

Auxiliary Space: O(n)

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