Created
          December 3, 2023 21:19 
        
      - 
      
- 
        Save dskecse/f58134c60ae44ba03b5d13f9813728b8 to your computer and use it in GitHub Desktop. 
    Hash (Dict) with a default value in Ruby vs Python
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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']) | 
  
    
      This file contains hidden or 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
    
  
  
    
  | 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