opposite of dict.items()

Discussion in 'Python' started by Tertius, Aug 28, 2003.

  1. Tertius

    Tertius Guest

    Is there a method to create a dict from a list of keys and a list of
    values ?

    TIA
    Tertius
     
    Tertius, Aug 28, 2003
    #1
    1. Advertising

  2. Tertius

    Peter Otten Guest

    Tertius wrote:

    > Is there a method to create a dict from a list of keys and a list of
    > values ?


    >>> dict(zip(range(3), "abc"))

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

    Not a method() but a method.

    Peter
     
    Peter Otten, Aug 28, 2003
    #2
    1. Advertising

  3. |a = {}
    |for k , v in zip(keys,values):
    | a[k] = v

    >>> keys = (1,2,3,4)
    >>> vals = 'a b c d'.split()
    >>> dct = dict(zip(keys,vals))
    >>> dct

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

    --
    mertz@ | The specter of free information is haunting the `Net! All the
    gnosis | powers of IP- and crypto-tyranny have entered into an unholy
    ..cx | alliance...ideas have nothing to lose but their chains. Unite
    | against "intellectual property" and anti-privacy regimes!
    -------------------------------------------------------------------------
     
    Lulu of the Lotus-Eaters, Aug 28, 2003
    #3
  4. Tertius

    Tertius Guest

    Peter Otten wrote:
    > Tertius wrote:
    >
    >
    >>Is there a method to create a dict from a list of keys and a list of
    >>values ?

    >
    >
    >>>>dict(zip(range(3), "abc"))

    >
    > {0: 'a', 1: 'b', 2: 'c'}
    >
    > Not a method() but a method.
    >
    > Peter
    >
    >


    much better than a for loop :)
    thnx
     
    Tertius, Aug 28, 2003
    #4
  5. [Tertius wrote]
    > > Is there a method to create a dict from a list of keys and a list of
    > > values ?


    [Peter Otten]
    > >>> dict(zip(range(3), "abc"))

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



    If you're using Py2.3, then the itertools way is a bit nicer:

    >>> dict(itertools.izip(range(3), "abc"))

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


    Raymond Hettinger
     
    Raymond Hettinger, Aug 29, 2003
    #5
  6. Tertius <> writes:

    > Tertius wrote:
    >> Is there a method to create a dict from a list of keys and a list of
    >> values ?
    >> TIA
    >> Tertius
    >>

    >
    > I found a way...
    >
    > a = {}
    > for k , v in zip(keys,values):
    > a[k] = v
    >


    Or:

    >python

    Python 2.3 (#1, Jul 30 2003, 21:59:29)
    [GCC 3.3 (SuSE Linux)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a=(1,2,3)
    >>> b=(4,5,6)
    >>> dict(zip(a,b))

    {1: 4, 2: 5, 3: 6}
    >>>


    Greetings
    Berthold
    --
    / http://starship.python.net/crew/bhoel/
    It is unlawful to use this email address for unsolicited ads
    (USC Title 47 Sec.227). I will assess a US$500 charge for
    reviewing and deleting each unsolicited ad.
     
    Berthold Hoellmann, Aug 29, 2003
    #6
  7. Tertius

    Chad Netzer Guest

    On Fri, 2003-08-29 at 04:53, Peter Otten wrote:

    > >>> dict(izip(irange(3), "abc"))

    > {0: 'a', 1: 'b', 2: 'c'}
    >
    > If I could use the above in Py2.4, it would be even nicer, namely:
    >
    > - convert itertools to builtins


    The push is to have LESS builtins. You can always import them in to the
    current namespace:

    from itertools import izip

    > - add irange(), perhaps as an alias for xrange() like file/open


    I suggested an irange to Raymond, and even coded one up, a while ago.
    He sees it as bloat, and rightfully so, since there is a push to allow
    optimizations of range that would achieve the same effect. (ie. rather
    than remembering when to use range(), xrange(), or irange(), we could
    just always use range() and the language would do lazy evaluation
    whenever possible.) Unless that avenue turns out to be a complete dead
    end, don't expect an irange().

    BTW. In the example you used, you COULD use xrange(). The only problem
    is that range has been extended, in 2.3, to accept longs, and xrange()
    only works with ints, and Guido wants it that way (he doesn't want to
    extend xrange()'s features, since it is a pain.) irange(), if it ever
    appears, would presumably be fully range() compatible.

    --
    Chad Netzer
     
    Chad Netzer, Aug 29, 2003
    #7
    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:
    424
    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:
    368
    Alexander Kozlovsky
    May 22, 2006
  3. Paul Melis

    dict.has_key(x) versus 'x in dict'

    Paul Melis, Dec 6, 2006, in forum: Python
    Replies:
    48
    Views:
    1,337
    Kent Johnson
    Dec 15, 2006
  4. Almad
    Replies:
    8
    Views:
    416
    Terry Reedy
    Dec 14, 2006
  5. Drew
    Replies:
    19
    Views:
    1,358
    Duncan Booth
    Mar 15, 2007
Loading...

Share This Page