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?
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.
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.
So, there you have it: Python uses SipHash because it's a trusted, cryptographic hash function that should prevent collision attacks.
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.
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:
nil
in Hash
, exception in dict
1
Hash
(since Ruby 2.0), no guarantee in dict
(until Python 3.6)1
Hash
only1
Hash
only2
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With