Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Ruby Hash and a Python dictionary?

In Python, there are dictionaries:

residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

In Ruby, there are Hashes:

residents = {'Puffin' => 104, 'Sloth' => 105, 'Burmese Python' => 106}

The only difference is the : versus => syntax. (Note that if the example were using variables instead of strings, then there would be no syntax difference.)

In Python, you call a dictionary's value via a key:

residents['Puffin']
# => 104

In Ruby, you grab a Hash's value via a key as well:

residents['Puffin']
# => 104

They appear to be the same.

What is the difference between a Hash in Ruby and a dictionary in Python?

like image 899
BenMorganIO Avatar asked May 09 '14 23:05

BenMorganIO


People also ask

Is hash same as dictionary in Python?

Hash tables or has maps in Python are implemented through the built-in dictionary data type. The keys of a dictionary in Python are generated by a hashing function. The elements of a dictionary are not ordered and they can be changed.

What is a Ruby hash?

In Ruby, Hash is a collection of unique keys and their values. Hash is like an Array, except the indexing is done with the help of arbitrary keys of any object type. In Hash, the order of returning keys and their value by various iterators is arbitrary and will generally not be in the insertion order.

What hash is used for Python dictionary?

So, there you have it: Python uses SipHash because it's a trusted, cryptographic hash function that should prevent collision attacks.

Is a dictionary just a hash table?

Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable.


1 Answers

Both Ruby's Hash and Python's dictionary represent a Map Abstract Data Type (ADT)

.. an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

Furthermore, both Hash and dictionary are implemented as Hash Tables which require that keys are hashable and equatable. Generally speaking, insert and delete and fetch operations on a hash table are O(1) amortized or "fast, independent of hash/dict size".

[A hash table] is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.

(Map implementations that use Trees, as opposed to Hash Tables, are found in persisted and functional programming contexts.)

Of course, there are also differences between Ruby and Python design choices and the specific/default Map implementations provided:

  • Default behavior on missing key lookup: nil in Hash, exception in dict1
  • Insertion-ordering guarantees: guaranteed in Hash (since Ruby 2.0), no guarantee in dict (until Python 3.6)1
  • Being able to specify a default value generator: Hash only1
  • Ability to use core mutable types (eg. lists) as keys: Hash only2
  • Syntax used for Hash/dict Literals, etc..

The [] syntax support is common insofar as both languages provide syntactic sugar for an overloaded index operator, but is implemented differently underneath and has different semantics in the case of missing keys.


1 Python offers defaultdict and OrderedDict implementations as well which have different behavior/functionality from the standard dict. These implementation allow default value generators, missing-key handling, and additional ordering guarantees that are not found in the standard dict type.

2 Certain core types in Python (eg. list and dict) explicitly reject being hashable and thus they cannot be used as keys in a dictionary that is based on hashing. This is not strictly a difference of dict itself and one can still use mutable custom types as keys, although such is discouraged in most cases.

like image 197
user2864740 Avatar answered Oct 15 '22 23:10

user2864740