Skip to content

Instantly share code, notes, and snippets.

@dskecse
Created December 3, 2023 21:19
Show Gist options
  • Save dskecse/f58134c60ae44ba03b5d13f9813728b8 to your computer and use it in GitHub Desktop.
Save dskecse/f58134c60ae44ba03b5d13f9813728b8 to your computer and use it in GitHub Desktop.
Hash (Dict) with a default value in Ruby vs Python
from collections import defaultdict
dict = defaultdict(lambda : 0)
dict["a"]
# => 0
dict.keys()
# => dict_keys(['a'])
print(dict.get("b"))
# => None
dict.keys()
# => dict_keys(['a'])
hash = Hash.new(0)
hash[:a]
# => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment