#!/usr/local/bin/python2 from itertools import ifilter l = ({1:1}, {2:1}, {3:1}, {4:1}, {5:5}, {6:1}, {7:1}, {8:1}, {9:1}) for i in xrange(500000): a = ifilter(e.get(5) for e in l) v = next(a) print v