error in dictionary , could not find why i cannot add c

35 Views Asked by At
from collections import defaultdict

    
# Defining the dict and passing
# lambda as default_factory argument
d = defaultdict(lambda c: "Not Present")
d["a"] = 1
d["b"] = 2


print(d["a"])
print(d["b"])
print(d["c"])

I expected a right outcome Everything sounds right Please check whats wrong with C lambda

1

There are 1 best solutions below

0
Sellig6792 On

The problem is the "c" in the lambda. You should use defaultdict like that:

d = defaultdict(lambda: "Not Present")

when you put a word after lambda, it's an argument. Exemple:

lambda x, y: x + y