- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create Python dictionary from the value of another dictionary?
You can do this by merging the other dictionary to the first dictionary. In Python 3.5+, you can use the ** operator to unpack a dictionary and combine multiple dictionaries using the following syntax −
Syntax
a = {'foo': 125} b = {'bar': "hello"} c = {**a, **b} print(c)
Output
This will give the output −
{'foo': 125, 'bar': 'hello'}
This is not supported in older versions. You can however replace it using the following similar syntax −
Syntax
a = {'foo': 125} b = {'bar': "hello"} c = dict(a, **b) print(c)
Output
This will give the output −
{'foo': 125, 'bar': 'hello'}
Another thing you can do is using copy and update functions to merge the dictionaries.
example
def merge_dicts(x, y): z = x.copy() # start with x's keys and values z.update(y) # modify z with y's keys and values return z a = {'foo': 125} b = {'bar': "hello"} c = merge_dicts(a, b) print(c)
Output
This will give the output −
{'foo': 125, 'bar': 'hello'}
- Related Articles
- Python – Create dictionary from the list
- How to create Python dictionary from JSON input?
- Python Program – Create dictionary from the list
- How to create a Python dictionary from text file?
- In Python how to create dictionary from two lists?
- How to create nested Python dictionary?
- How to create Python dictionary from list of keys and values?
- How to create a Pandas series from a python dictionary?
- How to create a dictionary in Python?
- How to extract subset of key-value pairs from Python dictionary object?
- Python program to create a dictionary from a string
- How to create a dictionary of sets in Python?
- How to convert Javascript dictionary to Python dictionary?
- How to create Python dictionary by enumerate function?
- How to create Python dictionary with duplicate keys?

Advertisements