Answers for "how to add element to list value in a dict python"

1

python append value to dictionary list

import collections

a_dict = collections.defaultdict(list) # a dictionary key --> list (of any stuff)
a_dict["a"].append("hello")

print(a_dict)
>>> defaultdict(<class 'list'>, {'a': ['hello']})

a_dict["a"].append("kite")

print(a_dict)
>>> defaultdict(<class 'list'>, {'a': ['hello', 'kite']})
Posted by: Guest on May-24-2021
0

how to add element to list value in a dict python

dates_dict.setdefault(key, []).append(date)
Posted by: Guest on October-08-2021

Code answers related to "how to add element to list value in a dict python"

Python Answers by Framework

Browse Popular Code Answers by Language