Use dict.setdefault()
:
dict.setdefault(key,[]).append(value)
help(dict.setdefault):
setdefault(...)
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
More Related Contents:
- How to map dict into another dict Python
- Create a list of names whose height is higher than 160 – Explanation on nested dicts cycling [closed]
- Determine the type of an object?
- Getting key with maximum value in dictionary?
- Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?
- multiprocessing: How do I share a dict among multiple processes?
- I’m getting Key error in python
- Merge and sum of two dictionaries
- Accessing elements of Python dictionary by index
- Order of keys in dictionary
- How do I merge two dictionaries in a single expression?
- Python dictionary : TypeError: unhashable type: ‘list’
- How to convert a nested Python dict to object?
- Write dictionary of lists to a CSV file
- join two lists of dictionaries on a single key
- How to sum all the values in a dictionary?
- In Python, how do I iterate over a dictionary in sorted key order?
- Difference between dict.clear() and assigning {} in Python
- Difference between defining typing.Dict and dict?
- How to json_normalize a column with NaNs
- Dictionary column in pandas dataframe
- JSON object must be str, bytes or bytearray, not dict
- How to convert a pymongo.cursor.Cursor into a dict?
- Are there any reasons not to use an OrderedDict?
- Dictionary vs Object – which is more efficient and why?
- Changing one dict changes all dicts in a list of dicts [duplicate]
- Why does dict(k=4, z=2).update(dict(l=1)) return None in Python? [duplicate]
- Rename columns in Pandas based on a dictionary
- How do I send list of dictionary as Body parameter together with Files in FastAPI?
- In what case would I use a tuple as a dictionary key? [closed]