Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61558 > unrolled thread

Re: grab dict keys/values without iterating ?!

Started byPeter Otten <__peter__@web.de>
First post2013-12-11 13:10 +0100
Last post2013-12-11 13:10 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: grab dict keys/values without iterating ?! Peter Otten <__peter__@web.de> - 2013-12-11 13:10 +0100

#61558 — Re: grab dict keys/values without iterating ?!

FromPeter Otten <__peter__@web.de>
Date2013-12-11 13:10 +0100
SubjectRe: grab dict keys/values without iterating ?!
Message-ID<mailman.3891.1386763843.18130.python-list@python.org>
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/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web