[Python-Dev] LinkedHashSet/LinkedHashMap equivalents

Steven Bethard steven.bethard at gmail.com
Wed Mar 9 00:43:34 CET 2005


Delaney, Timothy C (Timothy) <tdelaney at avaya.com> wrote:
> The perennial "how do I remove duplicates from a list" topic came up on
> c.l.py and in the discussion I mentioned the java 1.5 LinkedHashSet and
> LinkedHashMap. I'd thought about proposing these before, but couldn't
> think of where to put them. It was pointed out that the obvious place
> would be the collections module.
> 
> For those who don't know, LinkedHashSet and LinkedHashMap are simply
> hashed sets and maps that iterate in the order that the keys were added
> to the set/map. I almost invariably use them for the above scenario -
> removing duplicates without changing order.
> 
> Does anyone else think it would be worthwhile adding these to
> collections, or should I just make a cookbook entry?

I guess I'm -0 on this.

Though I was the one that suggested that collections is the right
place to put them, I'm not really certain how much we gain by
including them.  I too would only ever use them for removing
duplicates from a list.  But if we're trying to provide a solution to
this problem, I'd rather see an iterable-friendly one.  See a previous
thread on this issue[1] where I suggest something like:

def filterdups(iterable):
     seen = set()
     for item in iterable:
         if item not in seen:
             seen.add(item)
             yield item

Adding this to, say, itertools would cover all my use cases.  And as
long as you don't have too many duplicates, filterdups as above should
keep memory consumption down better.

On the other hand, if someone could give me a few other reasonable use
cases for LinkedHashSet and LinkedHashMap, I wouldn't object to their
inclusion.

Steve

[1] http://mail.python.org/pipermail/python-list/2005-February/264179.html

-- 
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list