• Register
0 votes
297 views

Problem :

Facing following python dictonary issue.
the elements in a dictionary are stored in ascending order, by the keys of the key-value pairs.
6 5 3
6,930 points

1 Answer

0 votes

Solution :

You should use OrderedDict (it requires Python 2.7 or higher).

Also please note that OrderedDict({'a': 1, 'b':2, 'c':3}) will not work since the dict you have create with {...} has already forgotten the order of the elements. So you should use OrderedDict([('a', 1), ('b', 2), ('c', 3)]).

If you follow above approach your issue will be resolved.

9 7 4
38,600 points

Related questions

1 vote
1 answer 11 views
11 views
Problem: I wrote a python program to check whether a given key already exists in a dictionary. The dictionary I have is: (If exists, print Key is present and the value of the key. Otherwise print key is not present .) {'a': 233, 'b':588, 'c':967 ... checkKey() missing 1 required positional argument: 'key' Whats the problem with my program can any one help me to sort this out? Thanks in advance.
asked Apr 26 samhaz 5k points
1 vote
1 answer 26 views
26 views
Problem: I am starting writing code in python. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "Present" else: print "Thanks" If there any other way to check this, please let me know.
asked Mar 10 Gavin 15.3k points
1 vote
1 answer 14 views
14 views
Problem: I have a dictionary that consists of: Dict = {     "Krishna" : 79,     "Arnav" : 46,     "Paulo" : 90,     "Christian" : 77,     "Renartovic" : 33,     "Wanda" : 65,     "Arteta" : 80,     "Santiago" : 68,     "Jovic" : 88     } I ... compiling this program:   File "val.py", line 13     if "Wanda" in Dict:     ^ IndentationError: unexpected indent Can anyone help me to solve this error?
asked Jun 7 samhaz 5k points
3 votes
1 answer 12 views
12 views
Problem: Hi all, I am trying to figure out the dictionary related problems in python. Dictionary is really fun to learn and work with. Let’s say, I have a dictionary dict = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} If ... needed to check every value inside the dictionary what can I do for that? So, I want to know how to check if a value is in a dictionary python?
asked Mar 24 Gavin 15.3k points
1 vote
1 answer 41 views
41 views
Problem : I was trying to check if key exists in dictionary python. So I wrote this code: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False ... unindent does not match any outer indentation level What is the problem in this program and how can I check if key exists in dictionary?
asked Apr 26 samhaz 5k points