Using Union and Dictionarie

Submitted 3 years, 1 month ago
Ticket #376
Views 308
Language/Framework Python
Priority Low
Status Closed

How can merge two disctionaries in a single expression by taking union of dictionarie ?

Submitted on Mar 23, 21
add a comment

1 Answer

Verified

dict_one = {"one": "one", "two": 2, "three": 3}

dict_two = {"three": 4, "five": 5}

# dict_one will be the union of both dictionaries
dict_one.update(dict_two) 

print(dict_one)

# as of Python 3.9, you can use:
dict_one |= dict_two  # similar to update() method

# Notice: All keys are unique inside a dictionary, so in case that
# both dictionaries have one or more identical keys, Python will use
# the values from the second dictionary for those keys

Submitted 3 years, 1 month ago


Latest Blogs