appending data to python dictionary
You could use
dict["key"] = value_list
so in your case:
mydict["key"] = z
as described here: Python docs
mydict = {}
print(mydict) # {}
Appending one key:
mydict['key1'] = 1
print(mydict) # {'key1': 1}
Appending multiple keys:
mydict.update({'key2': 2, 'key3': 3})
print(mydict) # {'key1': 1, 'key2': 2, 'key3': 3}
Append works for arrays, but not dictionaries.
To add to a dictionary use dict_name['item'] = 3
Another good solution (especially if you want to insert multiple items at once) would be: dict_name.update({'item': 3})
The NoneType error comes up when an instance of a class or an object you are working with has a value of None
. This can mean a value was never assigned.
Also, I believe you are missing a bracket here: mydict.setdefault(items,]).append(counts[j])
It should be: mydict.setdefault(items,[]).append(counts[j])