Open In App

Python – Strings with all given List characters

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

GIven Strings List and character list, extract all strings, having all characters from character list.

Input : test_list = [“Geeks”, “Gfg”, “Geeksforgeeks”, “free”], chr_list = [ ‘f’, ‘r’, ‘e’] 
Output : [‘free’, “Geeksforgeeks”] 
Explanation : Only “free” and “Geeksforgeeks” contains all ‘f’, ‘r’ and ‘e’. 

Input : test_list = [“Geeks”, “Gfg”, “Geeksforgeeks”, “free”], chr_list = [‘x’] 
Output : [] 
Explanation : No word contains ‘x’.

Method #1 : Using loop

In this, we iterate for all elements from list, and check if all are present in particular string, if yes, then that string is appended to result.

Python3




# Python3 code to demonstrate working of
# Strings with all List characters
# Using loop
 
# initializing list
test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char_list
chr_list = ['g', 'f']
 
res_list = []
for sub in test_list:
    res = True
    for ele in chr_list:
         
        # check if any element is not present
        if ele not in sub:
            res = False
            break
    if res:
        res_list.append(sub)
 
# printing results
print("Filtered Strings : " + str(res_list))


Output

The original list is : ['Geeks', 'Gfg', 'Geeksforgeeks', 'free']
Filtered Strings : ['Gfg', 'Geeksforgeeks']

Time Complexity: O(n*n), where n is the length of the input list. 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. 

Method #2 : Using all() + list comprehension

 In this, we check for all characters presence using all(), and if checks out, String is appended into result. Iteration part done in list comprehension as one-liner.

Python3




# Python3 code to demonstrate working of
# Strings with all List characters
# Using all() + list comprehension
 
# initializing list
test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char_list
chr_list = ['g', 'f']
 
# using all() to check containment of all characters
res_list = [sub for sub in test_list if all(ele in sub for ele in chr_list)]
 
# printing results
print("Filtered Strings : " + str(res_list))


Output

The original list is : ['Geeks', 'Gfg', 'Geeksforgeeks', 'free']
Filtered Strings : ['Gfg', 'Geeksforgeeks']

Method 3: Using the set and all functions. 

Step by step approach:

  1. Initialize the original list of strings.
  2. Initialize the list of characters to be checked for containment.
  3. Create an empty list to store the filtered strings.
  4. Convert the list of characters to be checked for containment into a set.
  5. Use a list comprehension to filter the strings that contain all the characters in the set.
  6. Print the original list and the filtered strings list.

Python3




# initializing list
test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char_list
chr_list = ['g', 'f']
 
# initializing filtered strings list
res_list = []
 
# convert the list of characters to be checked for containment into a set
chr_set = set(chr_list)
 
# use a list comprehension to filter the strings that contain all the characters in the set
res_list = [sub for sub in test_list if chr_set.issubset(set(sub.lower()))]
 
# printing results
print("Filtered Strings : " + str(res_list))


Output

The original list is : ['Geeks', 'Gfg', 'Geeksforgeeks', 'free']
Filtered Strings : ['Gfg', 'Geeksforgeeks']

Time complexity: O(n*m), where n is the number of strings in the original list and m is the length of the characters to be checked for containment.
Auxiliary space: O(k), where k is the number of strings that pass the filter.

Method 4: Use the filter() function and lambda function

Converting the list of characters to lowercase for case-insensitive matching using filter() function with a lambda function to filter the strings

Python3




# initializing list
test_list = ["Geeks", "Gfg", "Geeksforgeeks", "free"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing char_list
chr_list = ['g', 'f']
 
chr_list_lower = [c.lower() for c in chr_list]
 
res_list = list(filter(lambda x: all(c in x.
                 lower() for c in chr_list_lower), test_list))
 
# printing results
print("Filtered Strings : " + str(res_list))


Output

The original list is : ['Geeks', 'Gfg', 'Geeksforgeeks', 'free']
Filtered Strings : ['Gfg', 'Geeksforgeeks']

The time complexity of this approach is O(n * m), where n is the length of the test_list and m is the maximum length of a string in the test_list. 

 The auxiliary space complexity is O(k), where k is the length of chr_list. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads