import sys inp = {} solutions = [] def juzgfvzgv(poi, path, twice): nex = inp[poi] #print(nex) for i in nex: if i == 'end': solutions.append(path + [i]) continue elif i.islower() and i in path: if twice and i != 'start': juzgfvzgv(i, path + [i], False) else: continue else: juzgfvzgv(i, path + [i], twice) with open(sys.argv[1], 'r') as f: for i in f.readlines(): i = i.strip() temp1, temp2 = i.split('-', 1) inp.setdefault(temp1, list()).append(temp2) inp.setdefault(temp2, list()).append(temp1) juzgfvzgv('start', ['start'], True) for i in solutions: print(i) print(len(solutions))