0
    values = {
        4 : review,
        5 : enter,
        2 : help,
        9 : hello,
        1 : happy,
}

I would like to be able to SORT the dictionary keys so that

I get

    values = {
        1 : happy,
        2 : help,
        4 : review,
        5 : enter,
        9 : hello,
}

I then I want to extract the second part the words (happy, help,review,enter,hello) into a list, in their order!

I have tried

test = sorted(values)
print(test)

which results in:

[1,2,4,5,9] which is the correct order, but how to retrieve the string values?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – MooingRawr Sep 08 '17 at 16:56
  • You can't sort dictionaries. They are un-ordered data structures. – Christian Dean Sep 08 '17 at 16:58
  • There are also about a zillion answers to every conceivable variant of this question on [SO]. Just search for it. – pvg Sep 08 '17 at 17:00
  • 3
    Possible duplicate of [How can I sort a dictionary by key?](https://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key) – Aran-Fey Sep 08 '17 at 17:13
  • If you just want to traverse the dictionary in sorted-key order, you can use `for key, val in sorted(values.items()):` - `sorted()` produces a list but you shouldn't really need to keep it (note it won't change due to changes in the loop). – Joffan May 31 '21 at 14:03

3 Answers3

0

You can't "sort the dictionary" as dictionaries are unordered, but you can get a list of the values in the dictionary sorted by their respective keys (which is what I think you're getting at with "How do I retrieve the string values?") -- just like you got a list of the keys sorted by the themselves already.

Two ways come to mind, the more obvious:

v = [d[k] for k in sorted(d)]
print(v)

And the more efficient (of the two):

v = [v for (k,v) in sorted(d.items())]
print(v)

Both result in a list of

['happy', 'help', 'review', 'enter', 'hello']
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • It's not my downvote, but I'm pretty sure the reason(s) can be found on this list: 1) answered a no effort question 2) answered an obvious duplicate – Aran-Fey Sep 08 '17 at 17:02
  • I mean, I'll answer duplicates like this all day -- it takes 20 seconds to answer a duplicate of this complexity, but significantly longer to find *the original* question of this type. Meh. – jedwards Sep 08 '17 at 17:04
  • Well, the thing is we probably get this exact question 3 times a week, and we really don't need a few dozen duplicates of it. And questions with no answers and score < 0 will be automatically deleted by the roomba. – Aran-Fey Sep 08 '17 at 17:10
  • @Rawing no you're right -- I've been searching for dupe targets but come up pretty dry -- a close one is [this](https://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key) but not sure it's the best. – jedwards Sep 08 '17 at 17:15
  • "it takes 20 seconds to answer a duplicate of this complexity, but significantly longer to find the original question of this type." It also makes the site worse instead of better, barely helps the person asking and makes canonical answers harder to find for other people. So it's worse than meh. – pvg Sep 08 '17 at 17:30
0
values = { 4: "review",
    5 : "enter",
    2 : "help",
    9 : "hello",
    1 : "happy",}

print([values[key] for key in sorted(values.keys())])

output:

['happy', 'help', 'review', 'enter', 'hello']
Golden Lion
  • 3,840
  • 2
  • 26
  • 35
-2
test = sorted(values.items())
print(test)   

data = [val for (key,val) in sorted(values.items())]
print(data)
Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17