I try to use get
method of dictionaries to avoid an if-else
structure, but the method returns a None
object, whereas I want a list. My code :
dic = {}
words = ['foo', 'bar']
for word in words :
long = len(word)
dic[long] = dic.get(long, []).append(word)
I want to have (at the end of the loop) :
{3:['foo', 'bar']}
But I have :
AttributeError: 'NoneType' object has no attribute 'append'
Which means that get
returned a None
object (default value)... where I though it should return a void list if the key doesn't exists, a list filled withs some strings if the key exists. Any ideas ?