Created
July 6, 2019 05:08
-
-
Save eyaltrabelsi/76d60f71aa41c86aa3c7fd5688fc2ec2 to your computer and use it in GitHub Desktop.
Python trick using default dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import collections | |
>>> m = collections.defaultdict(int) | |
>>> m['a'] | |
0 | |
>>> m = collections.defaultdict(str) | |
>>> m['a'] | |
'' | |
>>> m['b'] += 'a' | |
>>> m['b'] | |
'a' | |
>>> m = collections.defaultdict(lambda: '[default value]') | |
>>> m['a'] | |
'[default value]' | |
>>> m['b'] | |
'[default value]' | |
>>> m = collections.defaultdict(list) | |
>>> m['a'] | |
[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment