maybe something like this
datadict = {} options = [({4:'foo', 5:'bar', 9:'xyzzy', 10:'yzzyx', 14:'Skaperen'}, (1, 2, 3)), ({4:'bar', 5:'foo', 9:'yzzyx', 11:'xyzzy', 14:'Skaperen'}, (2, 1, 3))] for line in sys.stdin: tokens = line.rstrip().split() for opt, get_tokens in options: if all(tokens[k] == value for k, value in opt.items()): processed += 1 datadict[tokens[0]] = tuple((tokens[i] for i in get_tokens)) break else: skipped += 1note I assume initial value of processed, skipped i set before the snippet you provide. Not sure if you really need processed (you can always check the len of datadict). maybe if you want to compare processed + skipped to total number of records to process?