[New-bugs-announce] [issue28071] Stop set.difference when set is empty

Terry J. Reedy report at bugs.python.org
Sat Sep 10 18:22:04 EDT 2016

New submission from Terry J. Reedy:

Proposal from SO: in the iteration loop for 'myset.difference(iterable)', add equivalent of "if not myset: break'.

In the toy example for testing that this is currently not so, myset starts empty, but it was noted by the OP that a more realistic example would be that myset becomes empty after deletions.

Postulated reasons why not to do this (as opposed to why it has not been done yet ;-):

1) averaged over all uses, the time saved not iterating would be less than the time spent testing emptyness.

2) an implicit guarantee to complete the iteration for possible side-effects.

One answer notes that myset.difference(anotherset) is special-cased and faster than the equivalent non-set iterable.

I searched the tracker for 'empty set difference' and got no hits.  If I remember, I will post any disposition of this issue back to SO.

messages: 275707
nosy: rhettinger, terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Stop set.difference when set is empty
type: performance
versions: Python 3.6, Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list