1

From what I understand, you can sort a dictionary by items, keys, and values which will then show the respected part in a sorted list

sorted( d.items...) shows the entire entry, keys to keys, and values to values.

How would I go about showing only the values in a dictionary based on the key being sorted?

I've tried sorted(d.values(), key = (lambda x:...) - but I'm not sure if I'm going about this the right way

d = { 2: (4,6) , 5: (3,2), 3: (5,9) } would be an example of a dictionary and I want it to display

{ (4,6), (5,9), (3,2) } - ordered based on key but only show values.

I'm trying to only use the sorted function and not import anything else

Same
  • 41
  • 1
  • 3
  • Ah, I forgot that I have the python-gold-badge. I only wanted to propose the duplicate instead of immediatly closing the question. However if the other question and answers don't solve your problem please include an explanation why not (and best also to provide an [mcve]) and I'll vote to reopen the question :) – MSeifert Apr 13 '17 at 19:50
  • 1
    Can you give an example input and output? – juanpa.arrivillaga Apr 13 '17 at 19:55
  • Oh I see, sorry about that. I tried reading that before but I'm not sure what to pull from that. I'll edit the original post. If it does though, I would appreciate it if I could be let known which one I should refer to. – Same Apr 13 '17 at 19:57
  • @Same you want `[d[k] for k in sorted(d)]` Note that this returns a list. You wrote a `set` literal, but I assume you meant a list (sets are unordered just like dictionaries). – juanpa.arrivillaga Apr 13 '17 at 21:05
  • Thank you! That was exactly what I was searching for, I appreciate it – Same Apr 13 '17 at 21:12

0 Answers0