Dictionary to tuple

Discussion in 'Python' started by Odd-R., Jun 28, 2005.

  1. Odd-R.

    Odd-R. Guest

    I have a dictionary, and I want to convert it to a tuple,
    that is, I want each key - value pair in the dictionary
    to be a tuple in a tuple.

    If this is the dictionary {1:'one',2:'two',3:'three'},
    then I want this to be the resulting tuple:
    ((1,'one'),(2,'two'),(3,'three')).

    I have been trying for quite some time now, but I do not
    get the result as I want it. Can this be done, or is it not
    possible?


    I must also add that I'm new to Python.

    Thanks in advance.



    --
    Har du et kjøleskap, har du en TV
    så har du alt du trenger for å leve

    -Jokke & Valentinerne
    Odd-R., Jun 28, 2005
    #1
    1. Advertising

  2. Odd-R. wrote:
    > I have a dictionary, and I want to convert it to a tuple,
    > that is, I want each key - value pair in the dictionary
    > to be a tuple in a tuple.
    >
    > If this is the dictionary {1:'one',2:'two',3:'three'},
    > then I want this to be the resulting tuple:
    > ((1,'one'),(2,'two'),(3,'three')).
    >
    > I have been trying for quite some time now, but I do not
    > get the result as I want it. Can this be done, or is it not
    > possible?


    It's of course possible, and even a no-brainer:

    dic = {1:'one',2:'two',3:'three'}
    tup = tuple(dic.items())

    The bad news is that dict are *not* ordered, so you'll have to sort the
    result yourself if needed :(

    The good news is that sorting a sequence is a no-brainer too !-)

    > I must also add that I'm new to Python.

    Welcome on board.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno modulix, Jun 28, 2005
    #2
    1. Advertising

  3. Odd-R.

    Jeff Epler Guest

    It looks like you want tuple(d.iteritems())

    >>> d = {1: 'one', 2: 'two', 3: 'three'}
    >>> tuple(d.iteritems())

    ((1, 'one'), (2, 'two'), (3, 'three'))

    You could also use tuple(d.items()). The result is essentially the
    same. Only if the dictionary is extremely large does the difference
    matter. (or if you're using an older version of Python without the
    iteritems method)

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQFCwWVSJd01MZaTXX0RAiFwAKChDHQYL/lUx6HEHJsbvqq9nSXI/gCfTvT2
    CdRYwJqNJH0Emnm86MMavms=
    =LyEz
    -----END PGP SIGNATURE-----
    Jeff Epler, Jun 28, 2005
    #3
  4. On 28 Jun 2005 14:45:19 GMT, Odd-R. <> wrote:
    > I have a dictionary, and I want to convert it to a tuple,
    > that is, I want each key - value pair in the dictionary
    > to be a tuple in a tuple.
    >
    > If this is the dictionary {1:'one',2:'two',3:'three'},
    > then I want this to be the resulting tuple:
    > ((1,'one'),(2,'two'),(3,'three')).
    >
    >>> d = {1:'one',2:'two',3:'three'}
    >>> t = tuple([(k,v) for k,v in d.iteritems()])
    >>> t

    ((1, 'one'), (2, 'two'), (3, 'three'))
    >>>
    Tim Williams (gmail), Jun 28, 2005
    #4
  5. Tim Williams (gmail) wrote:
    (snip)
    >>>>d = {1:'one',2:'two',3:'three'}
    >>>>t = tuple([(k,v) for k,v in d.iteritems()])


    Err... don't you spot any useless code here ?-)

    (tip: dict.items() already returns a list of (k,v) tuples...)

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno modulix, Jun 28, 2005
    #5
  6. bruno modulix wrote:

    > Err... don't you spot any useless code here ?-)
    >
    > (tip: dict.items() already returns a list of (k,v) tuples...)


    But it doesn't return a tuple of them. Which is what the tuple call
    there does.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    But when I reached the finished line / Young black male
    -- Ice Cube
    Erik Max Francis, Jun 28, 2005
    #6
  7. Odd-R.

    Robert Kern Guest

    Erik Max Francis wrote:
    > bruno modulix wrote:
    >
    >>Err... don't you spot any useless code here ?-)
    >>
    >>(tip: dict.items() already returns a list of (k,v) tuples...)

    >
    > But it doesn't return a tuple of them. Which is what the tuple call
    > there does.


    The useless code referred to was the list comprehension.

    >>> t = tuple([(k,v) for k,v in d.iteritems()])


    versus

    >>> t = tuple(d.items())


    or even

    >>> t = tuple(d.iteritems())


    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jun 28, 2005
    #7
  8. Erik Max Francis wrote:

    > But it doesn't return a tuple of them. Which is what the tuple call
    > there does.


    Yes, but I think he meant:

    t = tuple(d.items())

    --
    Jeremy Sanders
    http://www.jeremysanders.net/
    Jeremy Sanders, Jun 28, 2005
    #8
  9. Odd-R.

    John Roth Guest

    "bruno modulix" <> wrote in message
    news:42c16464$0$26260$...
    > Odd-R. wrote:
    >> I have a dictionary, and I want to convert it to a tuple,
    >> that is, I want each key - value pair in the dictionary
    >> to be a tuple in a tuple.
    >>
    >> If this is the dictionary {1:'one',2:'two',3:'three'},
    >> then I want this to be the resulting tuple:
    >> ((1,'one'),(2,'two'),(3,'three')).
    >>
    >> I have been trying for quite some time now, but I do not
    >> get the result as I want it. Can this be done, or is it not
    >> possible?

    >
    > It's of course possible, and even a no-brainer:
    >
    > dic = {1:'one',2:'two',3:'three'}
    > tup = tuple(dic.items())


    I think I'll add a little clarification since the OP is really new
    to Python. The (dict.items()) part of the expression returns a
    list, and if you want to sort it, then you need to sort the list
    and then convert it to a tuple.

    dic = {1:'one',2:'two',3:'three'}
    dic.sort()
    tup = tuple(dic)

    This points up the fact that the final conversion to a tuple
    may not be necessary. Whether or not is is depends on
    the circumstances.

    >> I must also add that I'm new to Python.

    > Welcome on board.
    >


    John Roth

    > --
    > bruno desthuilliers
    > python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    > p in ''.split('@')])"
    John Roth, Jun 28, 2005
    #9
  10. Erik Max Francis wrote:
    > bruno modulix wrote:
    >
    >> Err... don't you spot any useless code here ?-)
    >>
    >> (tip: dict.items() already returns a list of (k,v) tuples...)

    >
    > But it doesn't return a tuple of them. Which is what the tuple call
    > there does.


    Of course, but the list-to-tuple conversion is not the point here. The
    useless part might be more obvious in this snippet:

    my_list = [(1, 'one'), (2, 'two'), (3, 'three')]
    my_tup = tuple([(k, v) for k, v in my_list])


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno modulix, Jun 29, 2005
    #10
    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. Michal Mikolajczyk
    Replies:
    1
    Views:
    789
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    916
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    599
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    784
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    940
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page