how to compare list to text file and delete missing entries
<p>If I have a list with different values like <code>Eind, Shf, Asuf</code>.
And a text file with</p>

<pre><code>20:36:00 - Baarn
20:36:00 - Enschede
20:37:00 - Eindhoven
20:37:00 - Eind
20:37:00 - Shf
20:37:00 - Asuf
20:38:00 - Nijmegen
</code></pre>

<p>How do I delete the values in the list from the text file but delete the whole line.</p>

<p>This is your list:</p>

<pre><code>deletion_list = ["Eind", "Shf", "Asuf"]
</code></pre>

<p>First convert into a set in order to make faster searches:</p>

<pre><code>deletion_set = set(deletion_list)
</code></pre>

<p>Then filter it out:</p>

<pre><code># Open the file in a with statement to make sure it closes correctly
with open("input.txt") as input_file, open("output.txt", "w") as output_file:
    # Write to new file
    list(map(output_file.write,
             # Iterates over all lines in file and take those in set
             (line for line in input_file
              # Checks if line is in set
              if line.rpartition(" ")[-1] in deletion_set))
</code></pre>

<p>This is probably the best way. It's efficient and bug-less (with statements prevent unknown exceptions from keeping files open)</p>

python list text-files