title: If line not present in the text file - python
body: <p>I have a list with a set of strings and another dynamic list:</p>
  
  <pre><code>arr = ['sample1','sample2','sample3']
  applist=[]
  </code></pre>
  
  <p>I am reading a text file line by line, and if a line starts with any of the strings in arr, then I append it to applist, as follows:</p>
  
  <pre><code>for line in open('test.txt').readlines():
      for word in arr:
          if line.startswith(word):
              applist.append(line)
  </code></pre>
  
  <p>Now, if I do not have a line with any of the strings in the arr list, then I want to append 'NULL' to applist instead. I tried:</p>
  
  <pre><code>for line in open('test.txt').readlines():
      for word in arr:
          if line.startswith(word):
              applist.append(line)
          elif word not in 'test.txt':
              applist.append('NULL')
  </code></pre>
  
  <p>But it obviously doesn't work (it inserts many unnecessary NULLs). How do I go about it? Also, there are other lines in the text file besides the three lines starting with the strings in arr. But I want to append only these three lines. Thanks in advance!</p>