Thursday, 22 August 2013

coffeescript dictionary get default

coffeescript dictionary get default

In Python if you have a dictionary that consists of lists like
mylist = {'foo': [], 'bar':[3, 4]}
and if you want to add something into that lists you can do
mylist.setdefault('baz', []).append(5)
not to write
key, list_element = 'baz', 5
if key in mylist:
mylist[key].append(list_element)
else:
mylist[key] = [list_element]
is there an equivalent for this in Coffeescript?

No comments:

Post a Comment