Reversing a dict?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • krumblebunk@gmail.com

    Reversing a dict?

    Hi - further to my earlier query regarding partial matches (which with
    all your replies enabled me to advance my understanding, thanks), I
    now need to reverse a dict.

    I know how to reverse a list (with the reverse method - very handy),
    but it doesn't seem possible to reverse a dict.

    I suspect what I need to do is somehow go from:

    thelist=list(th edict)
    thelist.reverse ()
    thedict=dict(th elist)

    Does anyone know how to convert / or reverse a dict?

    thanks

    kb.



  • cokofreedom@gmail.com

    #2
    Re: Reversing a dict?

    On May 6, 5:20 pm, krumbleb...@gma il.com wrote:
    Hi - further to my earlier query regarding partial matches (which with
    all your replies enabled me to advance my understanding, thanks), I
    now need to reverse a dict.
    >
    I know how to reverse a list (with the reverse method - very handy),
    but it doesn't seem possible to reverse a dict.
    >
    I suspect what I need to do is somehow go from:
    >
    thelist=list(th edict)
    thelist.reverse ()
    thedict=dict(th elist)
    >
    Does anyone know how to convert / or reverse a dict?
    >
    thanks
    >
    kb.
    Issue 1: A dictionary is not ordered so cannot be reversed, as is.

    Saw something like this though:

    info = {"PHP":"17th May",
    "Perl":"15t h June",
    "Java":"7th June",
    "Python":"2 6th May",
    "Tcl":"12th July",
    "MySQL":"24 th May"}

    topics = info.keys()
    topics.sort()
    topics.reverse( )

    for topic in topics:
    print "Next",topic,"c ourse starts",info[topic]

    Comment

    • krumblebunk@gmail.com

      #3
      Re: Reversing a dict?

      Thanks all!!

      kb.

      Comment

      • Paul Hankin

        #4
        Re: Reversing a dict?

        On May 6, 4:24 pm, cokofree...@gma il.com wrote:
        On May 6, 5:20 pm, krumbleb...@gma il.com wrote:
        >
        >
        >
        Hi - further to my earlier query regarding partial matches (which with
        all your replies enabled me to advance my understanding, thanks), I
        now need to reverse a dict.
        >
        I know how to reverse a list (with the reverse method - very handy),
        but it doesn't seem possible to reverse a dict.
        >
        I suspect what I need to do is somehow go from:
        >
        thelist=list(th edict)
        thelist.reverse ()
        thedict=dict(th elist)
        >
        Does anyone know how to convert / or reverse a dict?
        >
        thanks
        >
        kb.
        >
        Issue 1: A dictionary is not ordered so cannot be reversed, as is.
        >
        Saw something like this though:
        >
        info = {"PHP":"17th May",
               "Perl":"15t h June",
               "Java":"7th June",
               "Python":"2 6th May",
               "Tcl":"12th July",
               "MySQL":"24 th May"}
        >
        topics = info.keys()
        topics.sort()
        topics.reverse( )
        >
        for topic in topics:
           print "Next",topic,"c ourse starts",info[topic]
        Better:

        for topic, when in sorted(topics.i teritems(), reverse=True):
        print 'Next %s course starts %s' % (topic, when)

        --
        Paul Hankin

        Comment

        Working...