Re: Help with creating a dict from list and range

Discussion in 'Python' started by James Stroud, Oct 15, 2005.

  1. James Stroud

    James Stroud Guest

    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}
    >
    > 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/


    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095

    http://www.jamesstroud.com/
     
    James Stroud, Oct 15, 2005
    #1
    1. Advertising

  2. "James Stroud" <> 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}



    "James Stroud" 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))

    George
     
    George Sakkis, Oct 15, 2005
    #2
    1. Advertising

  3. James Stroud wrote:
    > On Friday 14 October 2005 08:37, Steve Holden wrote:
    >> >>> dct = dict((x[1], x[0]) for x in enumerate(description))


    To make the code a breath more obvious:
    >>> dct = dict((name, seq) for seq, name in enumerate(description))

    has the same results.

    >> >>> dct

    >>
    >>{'second': 1, 'third': 2, 'first': 0}
    >>



    --
    -Scott David Daniels
     
    Scott David Daniels, Oct 15, 2005
    #3
  4. James Stroud

    Steve Holden Guest

    George Sakkis wrote:
    > "James Stroud" <> 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/
     
    Steve Holden, Oct 16, 2005
    #4
  5. Steve Holden wrote:

    > 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))


    a missing

    def enumerate(x, enumerate=enumerate): # override
    return (reversed(x) for x in enumerate(x))

    before the dict call?

    (dict(reversed(x) for x in enumerate(description)) isn't too bad, btw)

    </F>
     
    Fredrik Lundh, Oct 16, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    428
    Skip Montanaro
    Aug 15, 2003
  2. Alexander Kozlovsky

    dict!ident as equivalent of dict["ident"]

    Alexander Kozlovsky, May 21, 2006, in forum: Python
    Replies:
    5
    Views:
    379
    Alexander Kozlovsky
    May 22, 2006
  3. Drew
    Replies:
    19
    Views:
    1,369
    Duncan Booth
    Mar 15, 2007
  4. Menghan Zheng
    Replies:
    1
    Views:
    285
    alex23
    Apr 20, 2010
  5. Cameron Simpson
    Replies:
    6
    Views:
    366
    alex23
    Apr 21, 2010
Loading...

Share This Page