Using Union and Dictionarie

Submitted 6 months ago
Ticket #376
Views 105
Language 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 6 months ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message