Skip to content

Instantly share code, notes, and snippets.

@thomashikaru
Created October 10, 2021 19:57
Show Gist options
  • Save thomashikaru/89405835f0f218ad14ad3648a2332cd3 to your computer and use it in GitHub Desktop.
Save thomashikaru/89405835f0f218ad14ad3648a2332cd3 to your computer and use it in GitHub Desktop.
from collections import defaultdict
d = defaultdict(int)
print(d["Alice"]) # prints 0
d["Bob"] += 1
print(d["Bob"]) # prints 1
d = defaultdict(list)
d["John"].append("eggs")
print(d["John"]) # prints ["eggs"]
d = defaultdict(lambda: 100)
d["Alice"] += 1
print(d["Alice"]) # prints 101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment