can we sort the key value pairs in a dictionary?

0 votes
since the dictionary is unordered, is there a way to sort the dictionary?
Mar 22 in Python by Waseem
• 2,070 points
16 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

we can use OrderedDict

import collections

result = colections.OrderedDict(sorted(dictionaryname.items()))

answered Mar 22 by Mohammad
• 1,400 points

Related Questions In Python

+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,710 points
24 views
0 votes
1 answer

Find the largest value in a dictionary

import operator stats = {'a':1000, 'b':3000, 'c': 100} max(stats.iteritems(), ...READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
26 views
0 votes
1 answer

Obtaining a value when given a key in python dicionaries

We cannot. Dictionaries aren't meant to be ...READ MORE

answered May 31, 2018 in Python by Nietzsche's daemon
• 4,260 points
14 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
• 56,120 points
14 views
0 votes
1 answer

I have a dictonary in python how to access the value field?

dic={"car":["limo","sedan"]} print (dic.keys())    <-----------------------Fetch the key "car" print (dic['car'][0])   <------------------------Fetch ...READ MORE

answered Dec 19, 2018 in Python by Shuvodip
59 views
0 votes
1 answer

What is a dictionary in Python?

Dictionary can be understood as a key-value ...READ MORE

answered Mar 4 in Python by Priyaj
• 56,120 points
12 views
0 votes
0 answers

can you give an example to get a random sample dictionary?

And a few operations as well. READ MORE

May 10 in Python by Waseem
• 2,070 points
4 views
0 votes
1 answer

How to find count/length of dictionary values of list?

You should have used d.itervalues() instead and ...READ MORE

answered May 13 in Python by John
5 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
24 views
0 votes
1 answer

How can I find the square of a number in python?

You can use the exponentiation operator or ...READ MORE

answered 5 days ago in Python by Mohammad
• 1,400 points
8 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.