THIS IS MY FIRST ARTCLE!! (here have some knowledge on default dictionaries)

·

1 min read

Hiya my name is Alden!

A default dictionary is a dictionary where if a key doesn't exist in a dictionary, a key will be created and a default value will be assigned to it based on the function used to create the default dictionary.

Example: a default dictionary created with the List function

from collections import defaultdict
defaultlistdict = defaultdict(List)

defaultlistdict['a'].append("cool") # {a:["cool"]}

As shown above, I've accessed a key that has not yet been initialized and appended a string "cool" to its value.

This is very useful because now you don't have to do a check like

if(key in dictionary){
    dictionary[key].append(cool)
}
else:
    dictionary[key] = ["cool"]