Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'resulting': 0.04; 'array.': 0.07; 'bits': 0.07; 'key.': 0.07; 'works.': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'handled.': 0.09; 'insertion': 0.09; 'runtime': 0.09; 'python': 0.10; 'index': 0.13; 'value.': 0.15; "(i'm": 0.16; 'dictionaries': 0.16; 'key/value': 0.16; 'keyerror': 0.16; 'pair,': 0.16; 'pairs': 0.16; 'set,': 0.16; 'memory': 0.17; 'element': 0.18; 'found,': 0.18; 'skip': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; 'gather': 0.22; 'keys': 0.22; 'new,': 0.22; 'trying': 0.22; 'code.': 0.23; 'seems': 0.23; "python's": 0.23; 'implemented': 0.24; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'entries': 0.27; 'correct': 0.28; 'function': 0.28; 'actual': 0.28; 'dictionary': 0.29; 'hash': 0.29; 'array': 0.29; 'raise': 0.29; 'typically': 0.29; "i'm": 0.30; 'generally': 0.32; 'case,': 0.34; 'this?': 0.34; 'add': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'identified': 0.35; 'lists.': 0.35; 'replace': 0.35; 'there': 0.36; '(and': 0.36; 'actions': 0.36; 'subject:work': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thought': 0.37; 'version': 0.38; 'delete': 0.38; 'still': 0.40; 'some': 0.40; 'challenge': 0.61; 'suitable': 0.61; 'bridge': 0.63; 'linked': 0.63; 'more': 0.63; 'information': 0.63; 'believe': 0.66; 'power': 0.72; 'strings)': 0.84; 'underneath': 0.84; 'balances': 0.93; 'not:': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=8Vr+7+gfIExDmsbuJN/b0ghE0IIiAR/SeorH7OgLTr4=; b=XTBIs/WiGZMzVaDeSG2d9yc6KlzbecTJ359dQoyjhS+Bk8WeyTd19KYn4koH6k4RJI 6xbI2EYNvyepvtKQyq2T01sm1u9hi2PX0shxyBBPzExFaU6af2MQw9S+6gaGDfE1GcmN Vx1+eJSg1KPzSK1cniCp1euz5JWOadmFk1AGH8usF9qrKJY00u7jEZWpC7L35tQAUt2S g4/59sYiwtHx9yOG1p1RW7CeZyf7b1KxDqFB1FOhNuK+yYKlcEVSEGk8w1uktsCmYGI+ qVftdaRzkKGyjTmV1z3lWcn7Sp9yBdPbapSDY8hZRkH4YsNchBGtui4/rpxyiiIgt4TT JM9g== MIME-Version: 1.0 X-Received: by 10.107.148.135 with SMTP id w129mr12902759iod.52.1437069394856; Thu, 16 Jul 2015 10:56:34 -0700 (PDT) In-Reply-To: References: Date: Thu, 16 Jul 2015 12:56:34 -0500 Subject: Re: How does a dictionary work exactly? From: Skip Montanaro To: "yoursurrogategod@gmail.com" Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437069397 news.xs4all.nl 2873 [2001:888:2000:d::a6]:37989 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5775 X-Received-Body-CRC: 1174651971 Xref: csiph.com comp.lang.python:93944 > I was trying to see how some have implemented a hashtable. I took a gather at dictobject.h/.c. It seems that underneath it all it's a linked list and that is used in order to store the actual information (I'm looking at PyDictEntry.) > > Am I correct in my assumption or is there more to this? I'm still looking into how new entries are handled. The Python dictionary implementation has been pretty well optimized over the years, so it might not be the most easy-to-read code. You might actually try and latch onto a copy of dictobject.[ch] from a very old version of Python (1.5-ish). That will have much less in the way of speed tricks obfuscating the code. Very generally (I'm writing with a lot of water under the bridge since I last thought about this), a dictionary contains an array whose length is typically a power of two (2**n). When considering a key for insertion or lookup, a hash value is computed, and the last n bits of the resulting value are used as an index into that array. Each element of the array consists of a linked list of all the key/value pairs whose keys hash to that value. Once you've identified an element in the hash array, the linked list is traversed looking for the key. There are three operations: get, set, delete. Each operation has one of two actions to perform depending whether the key is found or not: * get - if found, return the corresponding value, if not, raise KeyError * set - if found, replace the old value with the new, if not, add a new key/value pair to the list * del if found, delete the key/value pair, if not, raise KeyError The challenge is to come up with a reasonable size hash array and a suitable hash function which balances the desire to not chew up all of memory with the desire to have very short lists. In Python's case, I believe that dictionaries with strings as keys (and the hash function used for strings) have been optimized for how Python's runtime works. Skip