[Python-ideas] Add orderedset as set(iterable, *, ordered=False) and similarly for frozenset. (original) (raw)

Raymond Hettinger raymond.hettinger at gmail.com
Sun Feb 8 04:26:01 CET 2015


Resending this earlier post. It was mistakenly sent to python-ideas at googlegroups.com instead of python-ideas at python.org.

Sorry for the confusion and for the posts arriving out-of-sequence.


On Feb 4, 2015, at 1:14 PM, Neil Girdhar <mistersheik at gmail.com> wrote:

Hundreds of people want an orderedset (http://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) and yet the ordered sets that are in PyPI (oset, ordered-set) are incomplete. Please consider adding an ordered set that has all of the same functionality as set. The only major design decision is (I think) whether to store the elements in a linked list or dynamic vector. My personal preference is to specify the ordered set via a flag to the constructor, which can be intercepted by new to return a different object type as necessary. (If anyone wants to add a complete ordered set to PyPI that would also be very useful for me!)

Long ago, I ago considered adding an OrderedSet() and decided to hold-off for several reasons:

What I suggest is adding the recipe (like the one shown below) directly to the docs in this section: https://docs.python.org/2/library/collections.html#ordereddict-examples-and-recipes There we already have code for a LastUpdatedOrderedDict() and an OrderedCounter().

In the past, I've used these recipes as a way to gauge interest. When the interest was sufficient, it got promoted from a recipe in the docs to a full implementation living in a standard library module. For example, itertools.combinations_with_replacement() and itertools.accumulate() started out as recipes in the docs.

In general, there shouldn't be a rush to push more things in the standard library. We get many suggestions in the form, "it might be nice to have XXX in the standard library", however, in many cases there has been so little uptake in real-world code that it is isn't worth the maintenance burden or for the burden on the working programmer for having too many choices in the standard library (I encounter very few people who actually know most of what is available in the standard library).

PyPI is a successful secondary source of code and has become part of the working programmer's life. We're gradually making it easier and easier to pull that code on-demand. IMO, that is where most of the collections variants should live. Otherwise, it would be easy to fill the collections module with dozens of rarely used dict/set variants, red-black trees, pairing heaps, tries, graph implementations, bitarrays, bitsets, persistent collections, etc.)

my-two-cents-ly,

Raymond

------- Rolling your own from existing parts --------------

from collections import OrderedDict, MutableSet

class OrderedSet(MutableSet):

def init(self, iterable=()): self._data = OrderedDict.fromkeys(iterable)

def add(self, key): self._data[key] = None

def discard(self, key): self._data.pop(key, None)

def len(self): return len(self._data)

def contains(self, key): return key in self._data

def iter(self): return iter(self._data)

def repr(self): return '%s(%r)' % (self.class.name, set(self))



More information about the Python-ideas mailing list