Python | Removing strings from tuple
Last Updated :
24 Mar, 2023
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
OutputThe 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)
Similar Reads
Python | Remove all strings from a list of tuples Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
8 min read
Python | Rear elements from Tuple Strings Yet another peculiar problem that might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This article solves the problem of extracting only the
5 min read
Python | Removing duplicates from tuple Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing
3 min read
Python | Convert tuple records to single string Sometimes, while working with data, we can have a problem in which we have tuple records and we need to change it's to comma-separated strings. These can be data regarding names. This kind of problem has its application in the web development domain. Let's discuss certain ways in which this problem
6 min read
Python | Remove prefix strings from list Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati
5 min read
How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways
2 min read
Convert String to Tuple - Python When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
Removing newline character from string in Python When working with text data, newline characters (\n) are often encountered especially when reading from files or handling multi-line strings. These characters can interfere with data processing and formatting. In this article, we will explore different methods to remove newline characters from strin
2 min read