Ian, Do you have some examples of things you can put in a set that you consider equal but want to store in the set INSTEAD of any current element?
What follows is some thoughts on some methods you could build yourself. Others re possible and someone else may present you with a module that does what you want. In principle, if you had a function that produced a canonical version of an argument as a sort of FIRST choice, you might still have to do some serious work. Assuming alpha, beta and gamma are all what you define as equal, such as if you consider the value of a string like "32.00" to be equal to "32.000000000" or even to "64/2", then you can get ALL elements of the set and call your function on it and compare it to the function applied to your new proposed set addition. So f(alpha)and f(beta) and f(gamma) would by definition return the same result, albeit that might be an integer like 32, or a string of "32.0" or whatever you need. Now are you sure the set has no "duplicates" already per your algorithm? If you are, your algorithm would need to loop on all keys and if a key matches using f() and the newer version is not otherwise identical, remove the old and insert the new. You can break out of the loop at that point BUT if there is no uniqueness guarantee, you need to process all keys every time. A possible improvement would be to consider various mixed approaches. You could use a dictionary where the keys are f(whatever) and the values are the current "whatever" as one example. The "set" then would be the values of the dictionary when you need that view, and the dictionary as a whole when you want to add or delete. If you needed operations like union and intersect, it may take more work. And, of course, you can maintain two sets. The other set would contain only f(whatever) while the first would contain the "whatever"s. What you are describing may have been implemented with a concept like partitions. An example might be the partitioning of numbers modulo N so that numbers are considered "equal" if they leave the same remainder when divided by N. Rather than use a set directly, this could be better done by creating an object that manages internal data structures so asking it to add a new number results in it calculating the remainder, seeing if it is in use, and replacing it with the latest if needed. Avi -----Original Message----- From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On Behalf Of Ian Pilcher Sent: Friday, December 30, 2022 4:41 PM To: python-list@python.org Subject: set.add() doesn't replace equal element I just discovered this behavior, which is problematic for my particular use. Is there a different set API (or operator) that can be used to add an element to a set, and replace any equal element? If not, am I correct that I should call set.discard() before calling set.add() to achieve the behavior that I want? -- ======================================================================== Google Where SkyNet meets Idiocracy ======================================================================== -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list