defaultdict in python

defaultdict:

Defaultdict is a container like dictionaries present in the module collections

The functionality of both dictionaries and defualtdict are almost same except for the fact that defualtdict never raises a KeyError. It provides a default value for the key that does not exists.

formation:

from collections import defaultdict

defaultdict(def_value)

def_value can be a function, a value, a lambda when the key does not exists.


example:

d = defaultdict(list)

the value part will be list

defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})


d = defaultdict(int)

the value part will be int

defaultdict(<class 'int'>, {1: 2, 2: 3, 3: 1, 4: 2})

Popular posts from this blog

Phonebook - Hack the box Write up -- Web LDAP injection

wafwaf -- Hack The Box -- Web SQL injection

Cheat sheet for security+