How to sort dictionary by value python

0 votes

Suppose my dictionary is as follows:

data = {1:'b', 2:'a', 3:'c'}

And I want to sort data by 'b' and 'a' so I get the result

'a','b','c'

How do I do that?
Any ideas?

Nov 2, 2018 in Python by findingbugs
• 3,260 points
286 views

1 answer to this question.

0 votes

This would work:

d = sorted(data, key = d.get)
for id in d:
    text = data[id]
answered Nov 2, 2018 by Nabarupa

Related Questions In Python

0 votes
1 answer

How to sort Counter by value using python?

Use the Counter.most_common() method, it'll sort the items for you: >>> ...READ MORE

answered May 23, 2018 in Python by charlie_brown
• 7,710 points
8,271 views
0 votes
1 answer

How do I sort a dictionary by value?

It is not possible to sort a ...READ MORE

answered Jul 30, 2018 in Python by Priyaj
• 58,060 points
254 views
0 votes
1 answer

How do I sort a dictionary by value?

If you construct a dictionary with the ...READ MORE

answered Mar 13, 2019 in Python by Trisha
188 views
0 votes
1 answer

How do I sort a list of dictionaries by a value of the dictionary?

Hello @kartik, import operator To sort the list of ...READ MORE

answered Apr 23, 2020 in Python by Niroj
• 82,680 points
206 views
0 votes
1 answer

Iterating over dictionaries using 'for' loops

key is just a variable name. for key ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,300 points
502 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,758 views
0 votes
1 answer

Sort Counter by value? - python

>>> x = Counter({'a':1, 'b':2, 'c':3}) >>> ...READ MORE

answered Nov 13, 2018 in Python by Priyaj
• 58,060 points
916 views
+1 vote
3 answers

How to change/update cell value in Python Pandas dataframe?

You can use the at() method to ...READ MORE

answered Apr 8, 2019 in Python by Kunal
138,285 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP