-2
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
    *default_factory ... is called without arguments ...* https://docs.python.org/3/library/collections.html#collections.defaultdict – ivvija Aug 30 '23 at 14:26
  • 1
    Change `lambda c: "Not Present"` to `lambda: "Not Present"` and you'll get the output you want. – Josh Clark Aug 30 '23 at 14:27
  • It looks like you don't want a defaultdict but rather a dictionary… `d = {'c': 'Not Present'}`, no? If you want 'Not Present' for all, then `d = defaultdict(lambda : "Not Present")` – mozway Aug 30 '23 at 14:40
  • Does this answer your question? [lambda function returning the key value for use in defaultdict](https://stackoverflow.com/questions/40931783/lambda-function-returning-the-key-value-for-use-in-defaultdict) – He3lixxx Sep 02 '23 at 12:03

1 Answers1

0

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
Sellig6792
  • 1
  • 1
  • 1