Skip to content
Related Articles

Related Articles

Python – Filter Tuples with Strings of specific characters

Improve Article
Save Article
  • Last Updated : 15 Nov, 2022
Improve Article
Save Article

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




# Python3 code to demonstrate working of
# Filter Tuples with Strings of specific characters
# Using all() + list comprehension
 
# initializing lists
test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing char_str
char_str = 'gfestb'
 
# nested all(), to check for all characters in list,
# and for all strings in tuples
res = [sub for sub in test_list if all(
    all(el in char_str for el in ele) for ele in sub)]
 
# printing result
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)

Space Complexity: 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




# Python3 code to demonstrate working of
# Filter Tuples with Strings of specific characters
# Using filter() + lambda + all()
 
# initializing lists
test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing char_str
char_str = 'gfestb'
 
# nested all(), to check for all characters in list,
# and for all strings in tuples filter() is used
# to extract tuples
res = list(filter(lambda sub: all(all(el in char_str for el in ele)
                                  for ele in sub), test_list))
 
# printing result
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)

Space Complexity: O(n)

Method #3 : Using replace(),join() and len() methods

Python3




# Python3 code to demonstrate working of
# Filter Tuples with Strings of specific characters
 
# initializing lists
test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# initializing char_str
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)
# printing result
print("The filtered tuples : " + str(res))


Output

The original list is : [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')]
The filtered tuples : [('gfg', 'best'), ('fest', 'gfg')]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!