RE: dict comprehension

Discussion in 'Python' started by Ryan Ginstrom, Feb 1, 2008.

  1. > On Behalf Of Daniel Fetchinson
    > What does the author mean here? What's the Preferably One Way
    > (TM) to do something analogous to a dict comprehension?


    I imagine something like this:

    >>> keys = "a b c".split()
    >>> values = [1, 2, 3]
    >>> D = dict([(a, b) for a, b in zip(keys, values)])
    >>> D

    {'a': 1, 'c': 3, 'b': 2}

    Regards,
    Ryan Ginstrom
    Ryan Ginstrom, Feb 1, 2008
    #1
    1. Advertising

  2. Ryan Ginstrom

    Paddy Guest

    On Feb 1, 6:06 am, "Ryan Ginstrom" <> wrote:
    > > On Behalf Of Daniel Fetchinson
    > > What does the author mean here? What's the Preferably One Way
    > > (TM) to do something analogous to a dict comprehension?

    >
    > I imagine something like this:
    >
    > >>> keys = "a b c".split()
    > >>> values = [1, 2, 3]
    > >>> D = dict([(a, b) for a, b in zip(keys, values)])
    > >>> D

    >
    > {'a': 1, 'c': 3, 'b': 2}
    >
    > Regards,
    > Ryan Ginstrom


    Hi Ryan, that uses a list comprehension.
    The generator comprehension is:
    D = dict((a, b) for a, b in zip(keys, values))

    (No square brackets)

    - Paddy.
    Paddy, Feb 1, 2008
    #2
    1. Advertising

  3. Ryan Ginstrom

    Paul McGuire Guest

    On Feb 1, 12:13 am, Paddy <> wrote:
    > On Feb 1, 6:06 am, "Ryan Ginstrom" <> wrote:
    >
    > > > On Behalf Of Daniel Fetchinson
    > > > What does the author mean here? What's the Preferably One Way
    > > > (TM) to do something analogous to a dict comprehension?

    >
    > > I imagine something like this:

    >
    > > >>> keys = "a b c".split()
    > > >>> values = [1, 2, 3]
    > > >>> D = dict([(a, b) for a, b in zip(keys, values)])
    > > >>> D

    >
    > > {'a': 1, 'c': 3, 'b': 2}

    >
    > > Regards,
    > > Ryan Ginstrom

    >
    > Hi Ryan, that uses a list comprehension.
    > The generator comprehension is:
    >   D = dict((a, b) for a, b in zip(keys, values))
    >
    > (No square brackets)
    >
    > - Paddy.


    Why not just

    D = dict(zip(keys,values))

    ??

    -- Paul
    Paul McGuire, Feb 1, 2008
    #3
  4. Ryan Ginstrom

    Guest

    Paul McGuire:
    > Why not just
    > D = dict(zip(keys,values))
    > ??


    Because this may require less memory:

    from itertools import izip
    D = dict(izip(keys, values))

    :)

    Bear hugs,
    bearophile
    , Feb 1, 2008
    #4
    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. Daniel Fetchinson

    dict comprehension

    Daniel Fetchinson, Feb 1, 2008, in forum: Python
    Replies:
    1
    Views:
    280
    Paul Rubin
    Feb 1, 2008
  2. Terry Reedy

    Re: dict comprehension

    Terry Reedy, Feb 1, 2008, in forum: Python
    Replies:
    6
    Views:
    389
    Arnaud Delobelle
    Feb 3, 2008
  3. Ernst-Ludwig Brust

    Dict Comprehension ?

    Ernst-Ludwig Brust, Oct 6, 2008, in forum: Python
    Replies:
    5
    Views:
    356
    Ernst-Ludwig Brust
    Oct 7, 2008
  4. Vedran Furac(
    Replies:
    4
    Views:
    322
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
  5. Peng Yu
    Replies:
    8
    Views:
    347
    Steven D'Aprano
    Nov 20, 2009
Loading...

Share This Page