Two dictionaries in a single expression in Python (

Submitted 3 years, 4 months ago
Ticket #322
Views 333
Language/Framework Python
Priority Low
Status Closed

How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)? 

Submitted on Dec 11, 20
add a comment

1 Answer

Verified

Let's say you have 2 dictionaries: dict1 and dict2.
You can merge dict2 inside dict1 using:
 

dict1.update(dict2.items())

update() method is used to add an item to a dictionary or to update an existing one
items() method will get all items from a dictionary as (key, value) pairs

Keep in mind that a dictionary can't have a key more than once.

Submitted 3 years, 4 months ago


Latest Blogs