for logs in mydir: for line in mylog: #...if the conditions are met list1.append(line) if any(True for line in list1 if "string" in line): list2.extend(list1) del list1 ....