0

I am beginning Python and I have read so many posts trying to figure this out. I have a dictionary made up of two lists. One of these is made up of integers and the other is a list of letters. The dictionary is sorted alphabetically however I want it to be sorted by the values associated with each letter. These values are not full integers. Could someone please let me in laymans terms how I can do this.

This is the dictionary

{
 'A': '0.03',
 'C': '0.02',
 'D': '0.04',
 'E': '0.06',
 'F': '0.05',
 'G': '0.03',
 'H': '0.03',
 'I': '0.08',
 'K': '0.07',
 'L': '0.14',
 'M': '0.02',
 'N': '0.06',
 'P': '0.03',
 'Q': '0.04',
 'R': '0.03',
 'S': '0.11',
 'T': '0.05',
 'V': '0.05',
 'W': '0.02',
 'Y': '0.03'
}

I would really appreciate a workaround that does not use any packages. Thank you very much in advance.

mhhabib
  • 2,975
  • 1
  • 15
  • 29
Andy
  • 5
  • 1
  • Why do you want to sort a dictionary at all? This makes only sense under rare circumstances. – Michael Butscher Nov 22 '20 at 19:09
  • If that doesn't answer your question, please [edit] to add what posts you've already read, what you've tried, and what hasn't worked. I'm also confused cause you mention lists and integers, but your data doesn't have either. BTW welcome to SO! Check out the [tour], and [ask] if you want tips. – wjandrea Nov 22 '20 at 19:10
  • Thanks wjandrea, it was good to check out the sites you recommended, another user provided a very good answer also, I was having difficulty using the sort function and making the key = lambda x, I wasn't sure how to approach it. Thanks for your help, I've bookmarked your links in case I have any more problems :) – Andy Nov 22 '20 at 19:29

3 Answers3

0

You can try this way supposing you stored your dictionary in variable data

{k: v for k, v in sorted(data.items(), key=lambda item: item[1])}

it outputs this

{'C': '0.02',
 'M': '0.02',
 'W': '0.02',
 'A': '0.03',
 'G': '0.03',
 'H': '0.03',
 'P': '0.03',
 'R': '0.03',
 'Y': '0.03',
 'D': '0.04',
 'Q': '0.04',
 'F': '0.05',
 'T': '0.05',
 'V': '0.05',
 'E': '0.06',
 'N': '0.06',
 'K': '0.07',
 'I': '0.08',
 'S': '0.11',
 'L': '0.14'}
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Aliasgher Nooruddin
  • 534
  • 1
  • 6
  • 18
  • Note that this requires Python 3.7+ or CPython 3.6. Reference: [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/q/39980323/4518341) – wjandrea Nov 22 '20 at 19:15
  • Hey, this worked perfectly, I had looked at the sorted function, but I kept getting errors that it couldn't deal with non-integers etc... I couldn't figure out how to apply it properly. Thank you so much :) – Andy Nov 22 '20 at 19:16
0

You could simply use sorted() with a lambda for this task and simply convert back to dict, without using a list comprehension to iterate over keys and values again. -

dict(sorted(d.items(), key=lambda x: x[1]))
{'C': '0.02',
 'M': '0.02',
 'W': '0.02',
 'A': '0.03',
 'G': '0.03',
 'H': '0.03',
 'P': '0.03',
 'R': '0.03',
 'Y': '0.03',
 'D': '0.04',
 'Q': '0.04',
 'F': '0.05',
 'T': '0.05',
 'V': '0.05',
 'E': '0.06',
 'N': '0.06',
 'K': '0.07',
 'I': '0.08',
 'S': '0.11',
 'L': '0.14'}

Incase you want it in descending order then -

dict(sorted(d.items(), key=lambda x: x[1], reverse=True))
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • 1
    Note that this requires Python 3.7+ or CPython 3.6. Reference: [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/q/39980323/4518341) – wjandrea Nov 22 '20 at 19:16
-1
d = {'A': '0.03', 'C': '0.02', 'D': '0.04', 'E': '0.06', 'F': '0.05', 'G': '0.03', 'H': '0.03', 'I': '0.08', 'K': '0.07', 'L': '0.14', 'M': '0.02', 'N': '0.06', 'P': '0.03', 'Q': '0.04', 'R': '0.03', 'S': '0.11', 'T': '0.05', 'V': '0.05', 'W': '0.02', 'Y': '0.03'}

def func(item):
    return item[1]

print( { k:v for k,v in sorted( d.items() , key= func ) } )

The key parameter of the sorted function takes a callback function which can be a lambda function or a regular callback function.

Askold Ilvento
  • 1,405
  • 1
  • 17
  • 20