How can I change the order in a dictionary | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I change the order in a dictionary

Hi there, I need help in this case, the problem is that I use a dictionary to generate a menu, and I want change the order of the menu, for example, if the user pick the option 6:"6. Choose favorite option on the menu" the order of the menu should change in such a way that: The favorite option get the first position and the rest of the options change the position. Other condition is that never the position 6 and 7 should change. My question is How can I change the order in a dictionary depending of a certain condition ? If you have the answer and you help me, thank u a lot! https://code.sololearn.com/cieWfCMl0E7B/?ref=app

17th Jun 2021, 2:55 PM
Brahian Suárez
Brahian Suárez - avatar
4 Answers
+ 5
Brahian Suárez , just to make sure i got it. let's say that we have a menu like: "1. input" "2. process" "3. output" now "3. output" should move to the top, and could be: "3. output" "1. input" "2. process" or it could be: "1. output" "2. input" "3. process" it would be helpful if you could give a sample like shown by me. thanks
17th Jun 2021, 4:04 PM
Lothar
Lothar - avatar
+ 2
Brahian Suárez , here is a kind of way how to do the task as you have described: (this uses python 3.7 and regular dict. it should not be too difficult if you like to change to ordered dict as visph mentioned) menu = { 1: "1. one", 2: "2. two", 3: "3. three", 4: "4. four", 5: "5. five" } print(menu) elem = 3 # element to move new_dict = { k:v for k,v in menu.items() if k == elem } # extract elem to move menu.pop(elem, None) # delete elem to move in menu dict new_dict.update(menu) # append menu dict with update() to new_dict print(new_dict) happy coding!
17th Jun 2021, 6:54 PM
Lothar
Lothar - avatar
+ 1
Lothar thank u for reply! Yeah, you got it, this is the think that I want make. For example: #This is my menu menu={ 1:"1.Hello" 2:"2.Good bye" 3:"3.Hi, there" } #Now I establish a variable Favorite= int(input("Choose a favorite option: ")) #Lets suppose that the user choose #The number 3 as a favorite option #So, the menu should be like this now: menu={ 3:"3.Hi, there" 2:"2.Good bye" 1:"1.Hello" } #I know that I can make this with a list, but I want to make this with a dictionary but I don't know if this is possible. I hope this time I was clear
17th Jun 2021, 4:41 PM
Brahian Suárez
Brahian Suárez - avatar
+ 1
depending on version of python you will/need support, you could choose between built-in dict or orderedDict from collections module... former keep key order since python 3.6, while later was designed at first to keep its keys ordered (python 3.1). however, orderedDict is probably the better choice in your case: greater backward compatibility, feature such as move_to_end() (python 3.2, not available for regular dict) and reversed() support for iterating over (python 3.5, regular dict only since 3.8) if you only need to support from python 3.5, it would be relatively easy to implement your menu feature with orderedDict in reverse order: add item in reverse order, print by iterating in reverse order, use move_to_end() to move any key to end of structure (first printed) ;) https://realpython.com/python-ordereddict/ https://docs.python.org/3/library/collections.html#collections.OrderedDict
17th Jun 2021, 6:29 PM
visph
visph - avatar