• Register
0 votes
423 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

Please log in or register to answer this question.

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

0 votes
1 answer 16 views
16 views
Problem: I tried to do the same as this post. The only problem is I don't know which keys and values are in advance. So I required to be able to dynamically add and exclude key and value pairs, and I don't know how to do that. Does anyone have a idea ... . thanks Is there anyone who can tell me how can i add dictionary and at same time add key-values pairs dynamically please someone help me ??
asked Nov 10, 2020 Han Li 710 points
0 votes
1 answer 6 views
6 views
Problem: Change value of key in dictionary python.
asked Mar 22 Dan phillip 4.7k points
0 votes
1 answer 17 views
17 views
Problem: I have dictionary: keys are strings, values are integers. For example: stats = {'a':1000, 'b':3000, 'c': 100} I would like to receive 'b'as an answer since this is the higher value key. I used the given data using an intermediate list with key-value inverse ... inverse = [(value, key) for key, value in stats.items()] print max(inverse)[1] Is this a better (or even more elegant) approach?
asked Dec 20, 2020 sasha 16.2k points
0 votes
1 answer 7 views
7 views
Problem: how to Solve this: error: which would you use to delete an existing key-value pair from a dictionary? HELP! I need help understanding this: which would you use to delete an existing key-value pair from a dictionary? Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
1 answer 12 views
12 views
Problem: How to I Solve this: count number of keys in dictionary python HELP! I need help understanding this: count number of keys in dictionary python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 3 Mashhoodch 13k points
0 votes
1 answer 9 views
9 views
Problem: 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 "blah" else: print "boo" I think this is not the best way to accomplish this task. Is there a better way to test for a key in the dictionary?
asked Jan 8 Mashhoodch 13k points
0 votes
1 answer 9 views
9 views
Problem: I used Google to try to find a solution on the web of the above question, But I got no useful hints. Can I get Pointer information about my problem with this?
asked Mar 21 rubelkhan 20.4k points
0 votes
1 answer 6 views
6 views
Problem: How to I Solve this: get list of keys from dictionary python HELP! I need help understanding this: get list of keys from dictionary python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points
0 votes
1 answer 5 views
5 views
Problem: Hello! I want to know that how can I add a new key and value to an existing dictionary? Python is quite new to me so please give a detailed explanation. Thanks!
asked Mar 24 Code Learner 9.2k points
0 votes
1 answer 19 views
19 views
Problem: I have a dictionary and I would like to get some values from it based on some keys. For example, I have a dictionary for users with their first name, last name, username, address, age and so on. Let's say, I only want to get one value (name) - either last ... if key exists, get value and stop checking. If not, move to next key. (3) username: if key exists, get value or return null/empty
asked Feb 21 Mashhoodch 13k points