As already explained the method append()
does not return value and therefore on the line:
dic['a'] = lista.append('a')
The key dic['a']
is assigned as None
.
If all the values in your dictionary are lists, the module Collections includes specialized container subclasses that facilitate coding.
The class defaultdict
is a subclass of dict
calling a Factory function to provide the values not found.
In the example is passed the constructor reference list()
as Factory function for defaultdict
:
from collections import defaultdict
dic = defaultdict(list)
dic['a'].append('a')
dic['a'].append('b')
dic['a'].append('c')
print(dic)
#defaultdict(<class 'list'>, {'a': ['a', 'b', 'c']})
Excellent contribution Magnun Leno!
– George Mendonça