Answer the question
In order to leave comments, you need to log in
How to check if a string exists in an array in python?
There is a file with lines:
If I do a check:
then the match is only on the last element of the array. As I understand it, there is an exact check, and not a check for availability. All elements of the array except the last one contain line breaks, which is why it happens. What is the easiest way to deal with such a situation? In php, I just trimmed the elements of the array:
blacklist = list(open('blacklist.txt'))
myline not in blacklist
array_map('trim', file('blacklist.txt'));
Answer the question
In order to leave comments, you need to log in
You are looking for a match of your line in the list, and you, as I understand it, need to be in the lines from this list. One solution:
arr = ['hello', 'world', 'nord']
any(filter(lambda s: 'ord' not in s, arr))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question