Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: fast dictionary initialization Date: Fri, 10 Jun 2016 18:20:14 -0500 Lines: 32 Message-ID: References: <20160610182014.0a9182a9@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Sg74eY2RjElXJ+N/rGmlBQ2U/r48wQUhwm6wSz4sOUkQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'dict': 0.09; 'example:': 0.10; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'received:10.104': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'silly': 0.16; 'storing': 0.16; 'wrote:': 0.16; 'translation': 0.16; 'string': 0.17; 'basically': 0.18; 'subject:skip:i 10': 0.22; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'function': 0.28; 'idea': 0.28; 'dictionary': 0.29; 'convert': 0.29; 'skip:[ 10': 0.31; 'similar': 0.33; 'mapping': 0.35; 'options:': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'where': 0.40; 'your': 0.60; 'worth': 0.67; 'received:23': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1465600816357:3103138998 X-MC-Ingress-Time: 1465600816357 In-Reply-To: X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20160610182014.0a9182a9@bigbox.christie.dr> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109811 On 2016-06-10 14:07, maurice wrote: > example: > valuesList = [1,2,3] > keysList = ['1','2','3'] > > So the dictionary can basically convert string to int: > > dictionary = {'1':1, '2':2, '3':3} A couple similar options: The most straightforward translation of your description: opt1 = dict(zip(keysList, valuesList)) print(opt1["2"]) And one where you generate the strings on the fly: opt2 = dict((str(i), i) for i in range(1, 4)) print(opt2["2"]) And one where you use the int() function instead of a mapping because the whole idea of storing a dict worth of string-numbers-to-numbers seems somewhat silly to me: print(int("2")) -tkc