How do you write a function that removes a string from an array if that string contains a certain character
For an example you would be removing all strings that contain an 'a'.
my_list = ["apples", "plums", "oranges", "lemons"]
How do you write a function that removes a string from an array if that string contains a certain character
For an example you would be removing all strings that contain an 'a'.
my_list = ["apples", "plums", "oranges", "lemons"]
It is simple as follows:
def get_filtered_list(my_list, sub_string):
return [string for string in my_list if sub_string not in string]
You can do this with list comprehension or a simple for
loop, the key is you want to check if 'a' not in something
, if there is an a
you don't want it
print([i for i in my_list if 'a' not in i])
Expanded:
for i in my_list:
if 'a' not in i:
print(i)