How to turn a list of tuples into a dictionary?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrstephengross

    How to turn a list of tuples into a dictionary?

    Let's say I've got a list of tuples, like so:

    ( ('a', '1'), ('b', '2'), ('c', '3')

    And I want to turn it into a dictionary in which the first value of
    each tuple is a key and the second value is a value, like so:

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

    Is there a way to do this with a single line of code? Currently, I'm
    doing it like this:

    tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
    d = {}
    for option,value in tuples: d[option] = value

    Thanks,
    --Steve

  • castironpi@gmail.com

    #2
    Re: How to turn a list of tuples into a dictionary?

    On Feb 26, 11:00 am, mrstephengross <mrstevegr...@g mail.comwrote:
    Let's say I've got a list of tuples, like so:
    >
      ( ('a', '1'), ('b', '2'), ('c', '3')
    >
    And I want to turn it into a dictionary in which the first value of
    each tuple is a key and the second value is a value, like so:
    >
      { 'a' -'1', 'b' -'2', 'c' -'3' }
    >
    Is there a way to do this with a single line of code? Currently, I'm
    doing it like this:
    >
      tuples =   ( ('a', '1'), ('b', '2'), ('c', '3')
      d = {}
      for option,value in tuples:  d[option] = value
    >
    Thanks,
    --Steve
    I'd hand-code it manually, by linking a C extension. Or
    dict( iterable ).


    Comment

    • Paul Rubin

      #3
      Re: How to turn a list of tuples into a dictionary?

      mrstephengross <mrstevegross@g mail.comwrites:
      ( ('a', '1'), ('b', '2'), ('c', '3')
      I'm not trying to be snide, but have you tried looking in the manual?

      See http://python.org/doc/lib and look at the section about built-in
      types, if you want to know things about tuples or dictionaries.

      Comment

      • D'Arcy J.M. Cain

        #4
        Re: How to turn a list of tuples into a dictionary?

        On Tue, 26 Feb 2008 09:00:54 -0800 (PST)
        mrstephengross <mrstevegross@g mail.comwrote:
        Let's say I've got a list of tuples, like so:
        >
        ( ('a', '1'), ('b', '2'), ('c', '3')
        >
        And I want to turn it into a dictionary in which the first value of
        each tuple is a key and the second value is a value, like so:
        >
        { 'a' -'1', 'b' -'2', 'c' -'3' }
        >
        Is there a way to do this with a single line of code? Currently, I'm
        doing it like this:
        >
        tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
        d = {}
        for option,value in tuples: d[option] = value
        How about this?
        d = dict(tuples)

        --
        D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
        http://www.druid.net/darcy/ | and a sheep voting on
        +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

        Comment

        • mrstephengross

          #5
          Re: How to turn a list of tuples into a dictionary?

          How about this?
          d = dict(tuples)
          Aha! I hadn't realized it could be so simple.

          --Steve

          Comment

          Working...