Python - Strings with all given List characters
Last Updated :
02 Jun, 2023
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))
OutputThe 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))
OutputThe original list is : ['Geeks', 'Gfg', 'Geeksforgeeks', 'free']
Filtered Strings : ['Gfg', 'Geeksforgeeks']
Method 3: Using the set and all functions.
Step by step approach:
- Initialize the original list of strings.
- Initialize the list of characters to be checked for containment.
- Create an empty list to store the filtered strings.
- Convert the list of characters to be checked for containment into a set.
- Use a list comprehension to filter the strings that contain all the characters in the set.
- 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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Key with all Characters in String
Sometimes, while working with Python Strings, we can have problem in which we need to extract all the keys that have all the characters of given some random key. This kind of problem has application has many domains such as day-day programming. Let's discuss a way in which this problem can be solved
3 min read
Python - Fill list characters in String
Given String and list, construct a string with only list values filled. Input : test_str = "geeksforgeeks", fill_list = ['g', 's', 'f', k] Output : g__ksf__g__ks Explanation : All occurrences are filled in their position of g, s, f and k. Input : test_str = "geeksforgeeks", fill_list = ['g', 's'] Ou
9 min read
Python | Pair the consecutive character strings in a list
Sometimes while programming, we can face a problem in which we need to perform consecutive element concatenation. This problem can occur at times of school programming or competitive programming. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + z
5 min read
Create a List of Strings in Python
Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Python | Remove given character from Strings list
Sometimes, while working with Python list, we can have a problem in which we need to remove a particular character from each string from list. This kind of application can come in many domains. Let's discuss certain ways to solve this problem. Method #1 : Using replace() + enumerate() + loop This is
8 min read
Python - Test String in Character List and vice-versa
Given a String, check if it's present in order in the character list and vice versa. Input : test_str = 'geeks', K = ['g', 'e', 'e', 'k', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] [ String in Character list ] Output : True Explanation : geeks is present in list , starting from 7th index till end. Inpu
3 min read
Iterate over characters of a string in Python
In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Python | Extract Strings with only Alphabets
In Python, extracting strings that contain only alphabetic characters involves filtering out any strings that include numbers or special characters. The most efficient way to achieve this is by using the isalpha() method, which checks if all characters in a string are alphabetic.Pythonli= ['gfg', 'i
2 min read
Python | Merge Range Characters in List
Sometimes, we require to merge some of the elements as single element in the list. This is usually with the cases with character to string conversion. This type of task is usually required in development domain to merge the names into one element. Letâs discuss certain ways in which this can be perf
6 min read
Python | String List to Column Character Matrix
Sometimes, while working with Python lists, we can have a problem in which we need to convert the string list to Character Matrix where each row is String list column. This can have possible application in data domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using
5 min read