70

I've always used dictionaries. I write in Python.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

7 Answers7

251

A dictionary is a general concept that maps keys to values. There are many ways to implement such a mapping.

A hashtable is a specific way to implement a dictionary.

Besides hashtables, another common way to implement dictionaries is red-black trees.

Each method has its own pros and cons. A red-black tree can always perform a lookup in O(log N). A hashtable can perform a lookup in O(1) time although that can degrade to O(N) depending on the input.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
38

A dictionary is a data structure that maps keys to values.

A hash table is a data structure that maps keys to values by taking the hash value of the key (by applying some hash function to it) and mapping that to a bucket where one or more values are stored.

IMO this is analogous to asking the difference between a list and a linked list.

For clarity it may be important to note that it MAY be the case that Python currently implements their dictionaries using hash tables, and it MAY be the case in the future that Python changes that fact without causing their dictionaries to stop being dictionaries.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
danben
  • 80,905
  • 18
  • 123
  • 145
  • Wouldn't the main difference be that a dictionary also stores the keys? So you can query a dictionary for th elsit of keys - you can't for a hash table – Martin Beckett Jan 14 '10 at 00:24
  • 3
    @Martin Beckett: Nope. Both can store the keys. The dictionary is general. The hash table is a specific implementation of the general concept. – S.Lott Jan 14 '10 at 00:24
  • @Martin Beckett: Hm, interesting point. Is it necessarily the case that a dictionary stores keys and a hash table doesn't? Java's `Hashtable` stores the keys - is it not a hash table, then? – danben Jan 14 '10 at 00:28
  • 4
    Thinking a bit more, a hashtable necessarily has to store the keys in case of collisions! – Martin Beckett Jan 14 '10 at 00:54
  • 2
    @danben: No. Storage of keys is not a distinguishing feature. The distinguishing feature is that a dictionary is a generic concept where a hashtable is an actual implementation. – S.Lott Jan 14 '10 at 01:05
  • A dictionary is a general concept--classes that store and retrieve values based on keys. A dictionary (in this interpretation--vague question) is *not* a data structure--the word doesn't imply any specific structure at all, but just a simple set of operations ("store", "retrieve", usually "enumerate"), which many actual data structures provide. – Glenn Maynard Jan 14 '10 at 05:57
  • @Glenn Maynard: What is the distinction here between data structure and concept? Can you give an example of a dictionary that is not a data structure? – danben Jan 14 '10 at 15:01
  • 1
    Would it be more acceptable to say abstract data structure? I have always used the two interchangeably. I think "concept" is getting a bit too vague. – danben Jan 14 '10 at 15:04
  • @danben In fact, storage of keys is needed for collision detection in any implementation where there can be collisions (bc the hash space is smaller than the actual space - this is most of cases) – ntg Jun 03 '20 at 07:51
18

"A dictionary" has a few different meanings in programming, as wikipedia will tell you -- "associative array", the sense in which Python uses the term (also known as "a mapping"), is one of those meanings (but "data dictionary", and "dictionary attacks" in password guess attempts, are also important).

Hash tables are important data structures; Python uses them to implement two important built-in data types, dict and set.

So, even in Python, you can't consider "hash table" to be a synonym for "dictionary"... since a similar data structure is also used to implement "sets"!-)

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
13

A Python dictionary is internally implemented with a hashtable.

Nicolás
  • 7,423
  • 33
  • 35
2

Both dictionary and hash table pair keys to value in order to have fast big O operations while insertion or deletion or lookups, the difference is that a hash table uses hash in order to store (key, value) pairs that's why we can access data faster. Python implements dictionaries as hash tables, Maps and sets are new kinds of hash tables that take into consideration the order while inserting, and you can put any kind of object as keys... Recently lists and hash table are more similar in Python3 due to order, Check this for more details: https://softwaremaniacs.org/blog/2020/02/05/dicts-ordered/en/

Nadin Martini
  • 193
  • 2
  • 4
  • 13
1

A hash table always uses some function operating on a value to determine where a value will be stored. A Dictionary (as I believe you intend it) is a more general term, and simply indicates a lookup mechanism, which might be a hash table or might be implemented by a simpler structure which does not consider the value itself in determining its storage location.

0

Dictionary is implemented using hash tables. In my opinion the difference between the 2 can be thought of as the difference between Stacks and Arrays where we would be using arrays to implement Stacks.

Thunderhashy
  • 5,291
  • 13
  • 43
  • 47