-1

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"]
sacuL
  • 49,704
  • 8
  • 81
  • 106
Kaine_G
  • 1
  • 2

2 Answers2

0

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]
Akif
  • 6,018
  • 3
  • 41
  • 44
0

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)
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20