with open("1.txt") as f1: f1_data = f1.readlines() with open("2.txt") as f2: f2_data = f2.readlines() print([int(n) for n in f1_data if n in f2_data])