49

I have a Python code that looks like:

if key in dict:
  dict[key].append(some_value)
else:
  dict[key] = [some_value]

but I figure there should be some method to get around this 'if' statement. I tried:

dict.setdefault(key, [])
dict[key].append(some_value)

and

dict[key] = dict.get(key, []).append(some_value)

but both complain about "TypeError: unhashable type: 'list'". Any recommendations?

5
  • Looks like you might be having trouble with list keys, unrelated to the whole default value thing. That, or you accidentally switched some arguments around in your actual code that you didn't when you posted it to SO. Jul 19, 2013 at 21:49
  • 1
    Your exception has nothing to do with the code you posted here. That indicates that key is a list object instead, which is not hashable and thus not permitted as a dictionary key.
    – Martijn Pieters
    Jul 19, 2013 at 21:53
  • 2
    In addition to Martijn's answer for setting defaults, you've run into the problem of using a python class name as a variable name. When you say dict.setdefault(key, []), you are actually calling the unbound setdefault method on the 'dict' class object. It treats 'key' as its self pointer and tries to use '[]' as an index. Just create your own variable mydict = dict() and you will get further.
    – tdelaney
    Jul 19, 2013 at 22:04
  • 1
    @tdelaney: From the rest of the post I surmise the OP has done dict = {} at some point. A bad idea, as that masks the built-in. The exception if dict is still the built-in is quite different: TypeError: descriptor 'setdefault' requires a 'dict' object but received a 'str' (for a str value in key).
    – Martijn Pieters
    Jul 19, 2013 at 22:09
  • Oh, wow. Big mistake on my part. I did not realize I was masking a built-in by writing dict = {}. My bad. Thanks!
    – Fysx
    Jul 28, 2013 at 15:44

1 Answer 1

110

The best method is to use collections.defaultdict with a list default:

from collections import defaultdict
dct = defaultdict(list)

Then just use:

dct[key].append(some_value)

and the dictionary will create a new list for you if the key is not yet in the mapping. collections.defaultdict is a subclass of dict and otherwise behaves just like a normal dict object.

When using a standard dict, dict.setdefault() correctly sets dct[key] for you to the default, so that version should have worked just fine. You can chain that call with .append():

>>> dct = {}
>>> dct.setdefault('foo', []).append('bar')  # returns None!
>>> dct
{'foo': ['bar']}

However, by using dct[key] = dct.get(...).append() you replace the value for dct[key] with the output of .append(), which is None.

1
  • 17
    This solution cut my execution time from 2hrs to 15min. I have extremely large dictionaries and the moment they reach 30 Million entries, they slow down due to all the checks if the key already exists. Also worth noting, i was using a networkx graph but the memory overhead was ridiculous. Now with the default dictionary the same graph that was taking 180GB to be loaded in RAM now takes less than 20GB using default dictionaries. You have saved a life
    – Chilli
    Mar 23, 2018 at 10:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.