Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Suggestion: make sequence and map interfaces more similar Date: Thu, 31 Mar 2016 13:41:19 -0400 Lines: 58 Message-ID: References: <56fb677f$0$11121$c3e8da3@news.astraweb.com> <56fba7d3$0$1616$c3e8da3$5496439d@news.astraweb.com> <56fbc518$0$1593$c3e8da3$5496439d@news.astraweb.com> <56fbf73d$0$1591$c3e8da3$5496439d@news.astraweb.com> <56fc8e09$0$1600$c3e8da3$5496439d@news.astraweb.com> <56fcfdc6$0$1612$c3e8da3$5496439d@news.astraweb.com> <56FD086F.2090301@rece.vub.ac.be> <56FD2158.2050406@rece.vub.ac.be> <87h9fmlor7.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 9jkGizgamiuDLF+z8kvQDQgMzBTTKwnBB6hp/pGG6lrQ== 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; 'corrections': 0.09; 'dict': 0.09; 'indexes': 0.09; 'lambda:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; 'def': 0.13; '10:13': 0.16; 'd[key]': 0.16; 'iterating': 0.16; 'iterator': 0.16; 'key:': 0.16; 'keyerror': 0.16; 'keyerror):': 0.16; 'keys.': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'set-like': 0.16; 'subject:interfaces': 0.16; 'subject:make': 0.16; 'subscripting': 0.16; 'wrote:': 0.16; 'pointed': 0.18; 'try:': 0.18; 'function:': 0.22; 'keys': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'skip:- 40': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; 'actual': 0.28; 'key,': 0.29; 'that.': 0.30; 'table': 0.32; 'raising': 0.33; 'except': 0.34; 'lists': 0.34; 'list': 0.34; 'could': 0.35; 'generic': 0.35; 'quite': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'default': 0.61; 'subject:more': 0.61; 'received:96': 0.63; 'different': 0.63; 'compose': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-96-227-207-81.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 In-Reply-To: <87h9fmlor7.fsf@elektro.pacujo.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106176 On 3/31/2016 10:13 AM, Marko Rauhamaa wrote: > One could compose a table of correspondences: with some corrections > ------------------------------------------- > list (L) dict (D) > ------------------------------------------- > L[key] = value D[key] = value > del L[key] (*) del L[key] > (*) reassigns all keys > key >= 0 and key < len(L) key in D '-len(L) <= key < len(L)' or 'key in range(-len(L), len(L)' Lists, tuples, and ranges have 2 keys for each value, though that is not guaranteed for sequences in general. key in D == key in D.keys() > range(len(L)) iter(D) iter(range(Len(L)) == iter(D.keys()) > L.clear D.clear > L.copy D.copy > lambda key: L[key] D.get The purpose of D.get() is to supply a default instead of raising KeyError when key not in D. The lambda function above does not do that. Turning subscripting into a function is a side-effect of D.get. A generic get function: def get(subscriptable, key, default=None): try: return subscriptable[key] except (IndexError, KeyError): return default > lambda: enumerate(L) D.items As I pointed out a couple days ago, an enumerate iterator is quite different from a set-like dynamic view. An actual correspondence: enumerate(L) iter(D.items()) Writing a set-like dynamic view of lists corresponding to D.values() or D.items() would be an interesting project. > lambda: range(len(L)) D.keys iter(range(len(L)) iter(D.keys()) Already given above. Iterating indexes is now much rarer than iterating dict keys. -- Terry Jan Reedy