import sys, collections filename = '%s' %(sys.argv[1]) with open(filename, newline='') as f: content = f.readlines() new_list = [] for line in content: if line.startswith('####'): title = line.strip().split('####')[1].split(' for ')[0] new_list.append(title) print(title) print([item for item, count in collections.Counter(new_list).items() if count > 1])