How to turn a defaultdict into a normal dict.

MRAB python at mrabarnett.plus.com
Wed Jun 10 16:05:20 EDT 2020


On 2020-06-10 09:05, Antoon Pardon wrote:
> The problem I face is that at the building face, I need a defaultdict
> because the values are lists that are appended too. So a
> defaultdict(list) is convenient in that new entries are treated as if
> the value is an empty list.
> 
> However later when I actually use it, accessing a key that is not
> present, should raise KeyError.
> 
> Is that somehow possible?
> 
Pass the defaultdict to dict:

my_dict = defaultdict(list)
...
my_dict = dict(my_dict)


More information about the Python-list mailing list