0
bad_words = ['Hi', 'hello', 'cool']
new_strings = []
for string in old_strings:
    if bad_words not in old_strings:
        new_strings.append(string)

how do I iterate through bad_words so that it doesnt include the strings that have them in it?

plum 0
  • 652
  • 9
  • 21
Blue
  • 51
  • 4

3 Answers3

3

Use any() with a list comprehension:

bad_words = ['Hi', 'hello', 'cool']
new_strings = [string 
               for string in old_strings 
               if not any(bad_word in string for bad_word in bad_words)]
Jan
  • 42,290
  • 8
  • 54
  • 79
1
bad_words = ['Hi', 'hello', 'cool']
new_strings = []
for string in old_strings:
    if string not in bad_words: 
        new_strings.append(string)

you question is not clear but i think this is the answer based on some assumptions

akhter wahab
  • 4,045
  • 1
  • 25
  • 47
0

I think you are using the wrong data structure. If you want unique values in a collection you should use a set instead of a list.

bad_words = {'Hi', 'hello', 'cool'} # this is a set

# now if you want to add words to this set, call the update method
new_strings = []
bad_words.update(new_strings)

You can always convert a set into a string as follows:

bad_words = {'Hi', 'hello', 'cool'}
l = list(bad_words)

For more information when to use a set/list/dict check this.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228