Given a Tuple List, extract tuples, which have strings made up of certain characters.
Input : test_list = [(‘gfg’, ‘best’), (‘gfg’, ‘good’), (‘fest’, ‘gfg’)], char_str = ‘gfestb’
Output : [(‘gfg’, ‘best’), (‘fest’, ‘gfg’)]
Explanation : All tuples contain characters from char_str.
Input : test_list = [(‘gfg’, ‘best’), (‘gfg’, ‘good’), (‘fest’, ‘gfg’)], char_str = ‘gfstb’
Output : []
Explanation : No tuples with given characters.
Method #1 : Using all() + list comprehension
In this, we check for characters in strings, using in operator and all() is used to check if all elements in tuple have strings made up of certain characters.
Python3
test_list = [( 'gfg' , 'best' ), ( 'gfg' , 'good' ), ( 'fest' , 'gfg' )]
print ( "The original list is : " + str (test_list))
char_str = 'gfestb'
res = [sub for sub in test_list if all (
all (el in char_str for el in ele) for ele in sub)]
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
The filtered tuples : [('gfg', 'best'), ('fest', 'gfg')]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + all()
Similar to the above method, difference being filter() + lambda is used to perform task of filtering.
Python3
test_list = [( 'gfg' , 'best' ), ( 'gfg' , 'good' ), ( 'fest' , 'gfg' )]
print ( "The original list is : " + str (test_list))
char_str = 'gfestb'
res = list ( filter ( lambda sub: all ( all (el in char_str for el in ele)
for ele in sub), test_list))
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
The filtered tuples : [('gfg', 'best'), ('fest', 'gfg')]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #3 : Using replace(),join() and len() methods
Python3
test_list = [( 'gfg' , 'best' ), ( 'gfg' , 'good' ), ( 'fest' , 'gfg' )]
print ( "The original list is : " + str (test_list))
char_str = 'gfestb'
res = []
for i in test_list:
x = "".join(i)
for j in char_str:
x = x.replace(j,"")
if ( len (x) = = 0 ):
res.append(i)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
The filtered tuples : [('gfg', 'best'), ('fest', 'gfg')]
Time complexity: O(n*m), where n is the length of the input list and m is the length of the char_str.
Auxiliary space: O(k), where k is the number of tuples that satisfy the condition, as we are storing those tuples in a new list.
Method #4 : Using re
One approach to solve this problem is using a regular expression. The idea is to join the tuples into a single string and then use re.search to check if all characters are in the character string. Here is the code:
Python3
import re
def filter_tuples(test_list, char_str):
res = []
for sub in test_list:
x = "".join(sub)
if re.search( "^[" + char_str + "]+$" , x):
res.append(sub)
return res
test_list = [( 'gfg' , 'best' ), ( 'gfg' , 'good' ), ( 'fest' , 'gfg' )]
char_str = 'gfestb'
print (filter_tuples(test_list, char_str))
|
Output
[('gfg', 'best'), ('fest', 'gfg')]
Time complexity: O(n^2)
Auxiliary Space: O(n)
Method #5: Using Set Operations
Initialize the input list and the characters string.
Convert the characters string into a set for faster lookups.
Iterate through each tuple in the input list.
Convert each tuple into a set of characters.
Use the set intersection operation to find the common characters between the tuple and the characters string.
Check if the length of the intersection set is equal to the length of the tuple. If it is, then all the characters in the tuple are present in the characters string.
If step 6 is true, append the tuple to the result list.
Return the result list.
Python3
test_list = [( 'gfg' , 'best' ), ( 'gfg' , 'good' ), ( 'fest' , 'gfg' )]
print ( "The original list is : " + str (test_list))
char_str = 'gfestb'
char_set = set (char_str)
res = []
for tup in test_list:
tup_set = set (''.join(tup))
if len (tup_set.intersection(char_set)) = = len (tup_set):
res.append(tup)
print ( "The filtered tuples : " + str (res))
|
Output
The original list is : [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
The filtered tuples : [('gfg', 'best'), ('fest', 'gfg')]
Time complexity: O(n*m), where n is the number of tuples and m is the length of the longest tuple. In the worst case, we need to iterate through every character in every tuple.
Auxiliary space: O(m), where m is the length of the longest tuple. We need to create a set for each tuple.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!