Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'assuming': 0.09; 'iterate': 0.09; 'lookup': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'subject:keys': 0.09; 'python': 0.11; 'stored': 0.12; 'columns': 0.16; 'dict': 0.16; 'entries,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:values': 0.16; 'url:sqlite': 0.16; 'wrote:': 0.18; 'select': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'url:dev': 0.24; 'right.': 0.26; 'task': 0.26; 'subject:/': 0.26; 'header:X-Complaints-To:1': 0.27; 'matching': 0.30; 'keys': 0.31; 'option': 0.32; 'another': 0.32; 'url:python': 0.33; 'table': 0.34; 'could': 0.34; 'but': 0.35; 'data,': 0.36; 'url:org': 0.36; 'two': 0.37; 'list': 0.37; 'starting': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'dave': 0.60; 'letters': 0.60; 'entire': 0.61; 'effectively': 0.66; '20.000': 0.84; 'lightweight': 0.84; 'absolutely': 0.87 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: grab dict keys/values without iterating ?! Date: Wed, 11 Dec 2013 13:10:52 +0100 Organization: None References: <52A7AB8C.8030700@arcor.de> <52A7AB8C.8030700@arcor.de> <52A8394C.5070402@arcor.de> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bc9e.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 1386763843 news.xs4all.nl 2926 [2001:888:2000:d::a6]:51326 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61558 Tamer Higazi wrote: > Hi Dave! > > You were absolutely right. > I don't want to iterate the entire dict to get me the key/values > > Let us say this dict would have 20.000 entries, but I want only those > with "Aa" to be grabed. > Those starting with these 2 letters would be only 5 or 6 then it would > take a lot of time. > > In which way would you prefer to store the data, and which functions or > methods would you use effectively to accomplish this task ? Well, Dave already gave one approach: [Dave Angel] > For example if you stored all the keys in a sorted list you could use > bisect. See also http://docs.python.org/dev/library/bisect.html Another option would be to use a database. Assuming the table 'lookup' has two columns 'key' and 'value' you'd get the matching rows with select key, value from lookup where key like 'Aa%'; A lightweight database that comes with Python is sqlite: http://docs.python.org/dev/library/sqlite3.html http://www.sqlite.org/