Is there a way to compare each list item to another list of exeptions?

Question:

I have two lists with strings. I need to compare each item in the first list to list of exeptions in the other list and replace items in the first list with ' ' if there is the same item in two lists. List of exeptions is in the .txt document. I’ve tried to make some code but it seems to work only with string list written in actual vs code.

with open('input.txt') as f:
    a = f.read().splitlines()
with open('exeptions.txt') as f:
    c = f.read().splitlines()
if c != []:
    for i in range(len(a)):
        if a[i] == 'exeption':
            a[i] = ' '

Example of txt files:

  • input.txt['a', 'b']
  • exeptions.txt['b']
  • end goal['a', ' ']
Asked By: wadwewe

||

Answers:

for i,thing in enumerate(a):
    if thing in b:
        a[i] = ''

If the empty string placeholder is not necessary then you could use a set

a = list(set(a).difference(b))
Answered By: Jordan Hyatt
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.