Skip to content
Advertisement

How to compare two dictionary and change the order of key if it matches

main_list = [“projecttype”, “emptype”, “Designation”]

main_list is to check the key exist in the dictionary or not

Primary Dict

JavaScript

Secondary Dict

JavaScript
  • Check the sample_P exist in the main_list
  • Check the sample_S exist in the main_list
  • Check if any key changes is there?
  • If changes then changed key will goes to last part of the dictionary
  • Only one key will change at one time

“emptype” key has changed in the secondary, if any change in the key it will goes to last

{"Designation":["Developer"], "emptype":["Manager"] }

Basically I need to check key’s in the both dictionaries if any changes in the dictionary key then it will goes to last

Code is below

JavaScript

Advertisement

Answer

You can iterate over the list of key if that is smaller and access the value of key using get. I have stored the changes result to a new dictionary.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement