difference between dictionary and hash table

7 篇文章 0 订阅

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

share improve this question
 

6 Answers

up vote 80 down vote accepted

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 it's 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.

share improve this answer
 
8 
I wish I could vote this one up more than once. –  LJM  Jan 14 '10 at 0:41
1 
I'll re-upvote it for you. –  Nicolás  Jan 14 '10 at 2:12

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 is 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.

share improve this answer
 
 
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 0:24
1 
@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 0: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 0:28
3 
Thinking a bit more, a hashtable necessarily has to store the keys in case of collisions! –  Martin Beckett  Jan 14 '10 at 0: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 1:05

"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"!-)

share improve this answer
 

A Python dictionary is internally implemented with a hashtable.

share improve this answer
 
 
A subclass of dict is not the Python implementation of a dictionary; it's your own
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值