Previous discussions on this: https://mail.python.org/archives/list/[email protected]/thread/ASHOHN32BQPBVPIGBZQRS24XHXFMB6XZ/ https://mail.python.org/archives/list/[email protected]/thread/K5SS62AB5DFFZIJ7ASKPLB2P3XGSYFPC/ (seems like part of the above discussion that got separated) https://mail.python.org/archives/list/[email protected]/thread/CKF2GFI3HKQAAYRCMMRPTFMONG3UGO4T/#CKF2GFI3HKQAAYRCMMRPTFMONG3UGO4T
On Thu, Jun 25, 2020 at 4:30 PM Ben Avrahami <[email protected]> wrote: > Hey all, > Often I've found this kind of code: > > seen = set() > for i in iterable: > if i in seen: > ... # do something in case of duplicates > else: > seen.add(i) > ... # do something in case of first visit > > This kind of code appears whenever one needs to check for duplicates in > case of a user-submitted iterable, or when we loop over a recursive > iteration that may involve cycles (graph search or the like). This code > could be improved if one could ensure an item is in the set, and get > whether it was there before in one operation. This may seem overly > specific, but dicts do do this: > > seen = {} > for i in iterable: > if seen.set_default(i, some_value) is not None: > ... # do something in case of duplicates > else: > ... # do something in case of first visit > > I think the set type would benefit greatly from its add method having a > return value. set.add would return True if the item was already in the set > prior to insertion, and False otherwise. > > Looking at the Cpython code, the set_add_entry already detects existing > entries, adding a return value would require no additional complexity. > > Any thoughts? > _______________________________________________ > Python-ideas mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/6WYNYNG5J5HBD3PA7PW75RP4PMLOMH4C/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/Y7TV4ZQOXE3BEIEDU7STKST4EL5GXUQW/ Code of Conduct: http://python.org/psf/codeofconduct/
