Help with creating a dict from list and range

Steve Holden steve at holdenweb.com
Sun Oct 16 06:54:11 EDT 2005


George Sakkis wrote:
> "James Stroud" <jstroud at mbi.ucla.edu> wrote:
  >>Could be even simpler since enumerate creates tuples anyway:
>>
>>dct = dict(x for x in enumerate(description))
>>
>>James
>>
>>On Friday 14 October 2005 08:37, Steve Holden wrote:
>>
>>> >>> dct = dict((x[1], x[0]) for x in enumerate(description))
>>> >>> dct
>>>
>>>{'second': 1, 'third': 2, 'first': 0}
> 
> 
> Or even simplest :-)
> 
> dct = dict(enumerate(description))
> 
You people should really mark these things as "untested" if you aren't 
going to bother actually producing a result that you can paste into your 
window (or at least checking that your solution really provides the 
answer the OP asked for).

Question: what's the difference between

    dict((name, seq) for seq, name in enumerate(description))

(the improved version of my answer posted by Scott David Daniels) and

    dict(enumerate(description))

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list