-1

Our use case is that if a key doesn't exist in the dictionary and we are trying to fetch the value against that key then a list with only that key should be returned as the default value.

Below is an example:

>>> dic = defaultdict(<function 'custom_default_function'>, {1: [1,2,6], 3: [3,6,8]})
>>> print(dic[1])
[1,2,6]
>>> print(dic[5])
[5]

In case of key with value 1 the output is completely fine as the key is there in dic. But for the case when we trying to look for key 5 then the default value that the code must print should be [5] i.e a list with only key as an element inside it.

I tried to write a default function but am not getting on how to pass parameter to the default function.

def default_function(key):
    return key
      
# Defining the dict
d = defaultdict(default_function)
d[1] = [1,4]
d[2] = [2,3]
print(d[4]) # This will throw error as the positional argument for default_function is not missing

Where am I going wrong and how can I resolve this using defaultdict in Python?

Alok Raj
  • 1,000
  • 9
  • 17
  • Thanks Alok. It is reassuring that you aren't expecting people to do your work. However, I feel I am doing a service by informing you, with kindness, that your phraseology tends towards "request for for free labour". I expect you would want to know. – halfer Apr 03 '22 at 12:37
  • In general it is best not to make requests for people to not edit your posts - for my sins I am the #2 editor on the site, and I can't accommodate special requests. I am not sure how I would keep track! – halfer Apr 03 '22 at 12:38
  • Just to know your judgement on this , could you please let me know what's wrong in the statement :- "Could anyone please help me with what am I doing wrong and how can I resolve this using defaultdict in python.Python?" I saw you edited this line. I want to first understand your perspective on why this seemed wrong to you and what you did is correct – Alok Raj Apr 03 '22 at 14:24
  • Not wrong exactly - it is _understandable_. But it is slightly needy, and moreover it is about what other people can do for you, not what you can do for you. "How can I do X" indicates to readers that you are aware that, once you get help here, the bulk of the task is still yours to do. – halfer Apr 03 '22 at 15:11

1 Answers1

3

defaultdict will not generate a new value that depends on the key...

you could inherit from dict and overload __missing__:

class MyDict(dict):

    def __init__(self):
        super().__init__()

    def __missing__(self, key):
        self[key] = [key]
        return self[key]

my_dict = MyDict()

print(my_dict[5])  # -> [5]
print(my_dict)     # -> {5: [5]}

there are 2 other answers here that might help:

hiro protagonist
  • 40,708
  • 13
  • 78
  • 98
  • thanks a lot for the solution, it really helped me a lot. I have one small doubt, when I am trying to inherit collections.defaultdict instead of dict and print the my_dict, I am getting the result as "MyDict(None, {5: [5]})" . Can you please explain me why None is there – Alok Raj Mar 20 '22 at 13:59
  • `defaultdict` prints the *factory* (i.e. the function that is called in order to create the default argument). – hiro protagonist Mar 20 '22 at 15:34