Dictionary from list?

Michael Hudson mwh at python.net
Fri Oct 19 12:26:03 CEST 2001


Ignacio Vazquez-Abrams <ignacio at openservices.net> writes:

[perl's %hash = @array]
> > (You could probably do something horrible with reduce, but don't).
> 
> No, I don't think that reduce() could help, although list comprehensions
> might.

OK, you asked for it <wink>:

/>> def beargh(d):
|..     unique = []
|..     def ouch(x,y):
|..         if x is unique:
|..             return y
|..         else:
|..             d[x] = y
|..             return unique
|..     return ouch
\__
->> d = {}
->> reduce(beargh(d), ['a', 1, 'b', 2])
[]
->> d
{'a': 1, 'b': 2}

you may have underestimated what I meant by horrible :-)

Cheers,
M.

-- 
  Counting lines is probably a good idea if you want to print it out
  and are short on paper, but I fail to see the purpose otherwise.
                                        -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list