Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #defaultdict will not always generate default value
- #if you didn't assign default data type to it, using defaultdict[key] to find the key might get an error
- #defaultdict has default data type
- >>> x = defaultdict(int)
- >>> x
- defaultdict(<type 'int'>, {})
- >>> x["abc"]
- 0
- >>> x
- defaultdict(<type 'int'>, {'abc': 0})
- #defaultdict has no default data type
- >>> x = defaultdict()
- >>> x
- defaultdict(None, {})
- >>> x["abc"]
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- KeyError: 'abc'
- #for safety, instead, we can use get() to find the key
- >>> x.get("abc") #will return nothing, but will not cause error
- >>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement