I need a dictionary that is composed by a lot of keys, and the values must be a list that contains lots of strings. (in Python)
I tried:
d1[key].append(value)
but Python says:
AttributeError: 'str' object has no attribute 'append'.
I need something like : {a:[b,c,d,e],b:[t,r,s,z]....}
What could I do?
thanks in advance.
You can use a collections.defaultdict:
>>> from collections import defaultdict
>>>
>>> d = defaultdict(list)
>>> d['key'].append(5)
>>> d
defaultdict(<type 'list'>, {'key': [5]})
>>> dict(d)
{'key': [5]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With