You could take the length of the set before and after the add()
. If it didn't change, the format was already in the set.
seen = set()
for (value, fmt) in formats:
l1 = len(seen)
seen.add(fmt)
if l1 != len(seen):
process(value, fmt)
Your question presumes that the in
test is a costly operation. This turns out not to be the case. Using len()
can take more time, although both are quite fast;
In [4]: seen = set(range(10000))
In [5]: %timeit 5995 in seen
10000000 loops, best of 3: 122 ns per loop
In [6]: %timeit len(seen)
1000000 loops, best of 3: 167 ns per loop
(measured with CPython 2.7.3 on a 2.5 GHz Core Quad Q9300)