python: merging dictionaries by identical value of key [duplicate]

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ How to merge two dictionaries in a single expression?_x000D_ _x000D_ 41 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ I am new to python and have looked at other answers on merging dictionaries but was still a bit confused. I am looking to merge together two dictionaries in python by a common value within a specific key to output that common key with the other keys from both dictionaries in a new dictionary. Here is sample data: add_sal = {'career_medicine': None, 'career_law': None, 'median_salary': None, 'mean_salary': 75000.0, 'career_business': 'operations / logistics', 'number': None} add_perc = {'percent': 0.07, 'career_business': 'operations / logistics'} I would like to merge on the key value pair of 'career_business': 'operations / logistics' and output a dictionary that looks like this: add_all = {'career_medicine': None, 'career_law': None, 'median_salary': None, 'mean_salary': 75000.0, 'career_business': 'operations / logistics', 'number': None, 'percent': 0.07} An additional problem is that I do not know if the names will match, and I am looping through a list of add_sal and a list of add_perc. Any advice would be appreciated! Thank you in advance!

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ What you asked for is simply enough done: import copy if 'career_business' in add_sal and 'career_business' in add_perc and \ add_sal['career_business'] == add_perc['career_business']: add_all = copy.deepcopy( add_sal ) add_all['percent'] = add_perc['percent'] However, your data structure seems rather odd for the kind of data you seem to have. You don't say what problem you're trying to solve with it, but your choice of a dictionary with random looking things in it seems likely to be at the root of your problems. Maybe you wanted something more like a dictionary keyed by the career name, like: career_sal[ 'operations / logistics' ] = 75000.0 and corresponding stuff for the other pieces.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community