Python has this feature built-in:
>>> d = {'b': 4}
>>> d.update({'a': 2})
>>> d
{'a': 2, 'b': 4}
Or given you’re not allowed to use dict.update
:
>>> d = dict(d.items() + {'a': 2}.items()) # doesn't work in python 3
More Related Contents:
- How to select random key with common values?
- How to remove [ ] and ” from Python output [duplicate]
- Are dictionaries ordered in Python 3.6+?
- Why is dictionary ordering non-deterministic?
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- count the number of occurrences of a certain value in a dictionary in python?
- TypeError: ‘dict_keys’ object does not support indexing
- Accessing dict_keys element by index in Python3
- What is the purpose of collections.ChainMap?
- One liner: creating a dictionary from list with indices as keys
- Accessing items in an collections.OrderedDict by index
- Accessing dictionary items by position in Python 3.6+ efficiently
- Unpack dictionary from Pandas Column
- Why is a class __dict__ a mappingproxy?
- Extract a subset of key-value pairs from dictionary?
- Creating an Ordered Counter
- Will OrderedDict become redundant in Python 3.7?
- Are there any reasons not to use an OrderedDict?
- How to get count dict of items but maintain the order in which they appear?
- How do I return dictionary keys as a list in Python?
- How to reverse a dictionary (whose values are lists) in Python? [duplicate]
- Changing one dict changes all dicts in a list of dicts [duplicate]
- Python dictionary replace values
- enumerate() for dictionary in Python
- Appending to list in Python dictionary [duplicate]
- Python – How to convert only numbers in a mixed list into float?
- How to convert string representation of dictionary in Pandas DataFrame to a new columns?
- How to convert only numbers in a mixed list into float
- How to add an element to the beginning of an OrderedDict?
- TypedDict when keys have invalid names