language design question

Alex Martelli aleax at mac.com
Sun Jul 9 23:34:50 EDT 2006


Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> wrote:
   ...
> >     This would allow things like:
> >         key = '',join( list(word.lower().strip()).sort() )
> 
> key = ''.join(list(sorted(word.lower().strip()))

No need to make yet another list here (also, I think each of you omitted
a needed closed-paren!-) -- you could just use:

key = ''.join(sorted(word.lower().strip()))


> >    - Another feature I assumed but it failed, is a nice default for
> > dictionaries, and more += like operations;
> >    For example: to acculumate words in a dictionary -
> >         dict[key] += [word]
> > 
> >      Instead of:
> >         mark[key] = mark.get(key,[]) + [word]
> 
> mark.setdefault(key, []).append(word)

setdefault was a worthy experiment, but it works rather clumsily in real
life -- most of us are back to splitting such "multidict assignments"
into two statements, such as:
    if key in mark: mark[key].append(word)
    else: mark[key] = [word]

Fortunately, 2.5's collections.defaultdict makes it pretty again (and
VASTLY more general than the one and only "nice default" the OP is
wishing for;-).
    

Alex



More information about the Python-list mailing list