0

I am trying to build a defaultdict which is initialized with a specific list. Such that if I access the dict using a key that does exist, it will be initialized with a specific list, let's say [True, True, True].

Instead of doing this

my_defaultdict = collections.defaultdict(list)

So, for example, something like this (obviously would not work)

my_defaultdict = collections.defaultdict([True, True, True])

I tried something like this, but this does not work

my_defaultdict = collections.defaultdict(lambda: list[True, True, True])

I looked in this question for a start, but could not figure it out.

Community
  • 1
  • 1
user2015487
  • 2,075
  • 3
  • 17
  • 20

1 Answers1

2

defaultdict's argument should be a function or any callable object:

my_defaultdict = collections.defaultdict(lambda : [True,True,True])
Assem
  • 11,574
  • 5
  • 59
  • 97