with open("input.txt","r") as f: lines = f.read().splitlines() def check(input): expiry_raw = (split[2].split(" = ")[1]).replace("[", "").replace("]","").split(",") for item in expiry_raw: if int(item.split("/")[1]) >= 21: return True break return False for line in lines: split = line.split(" | ") split1 = line.split("|") if "ending" in split[1]: if check(line) == True: with open("output.txt","a") as f: f.write(line+"\n") print(line)