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?
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?
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)]
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
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.