Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'retrieved': 0.05; 'interpreter': 0.07; 'python': 0.08; 'indexes': 0.09; 'integers': 0.09; 'subject:Not': 0.09; 'am,': 0.13; 'wrote:': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'rao': 0.16; 'rows': 0.16; 'subject:limitation': 0.16; 'subject:memory': 0.16; 'received:74.125.82.44': 0.19; 'received:mail- ww0-f44.google.com': 0.19; 'subject:data': 0.22; 'header:In-Reply- To:1': 0.22; 'dictionary': 0.23; 'module,': 0.23; 'string': 0.26; 'function': 0.26; '(in': 0.26; 'message-id:@mail.gmail.com': 0.28; 'import': 0.29; 'do.': 0.30; 'strings,': 0.30; 'strings.': 0.30; 'sun,': 0.30; "won't": 0.32; 'chris': 0.32; 'earlier': 0.32; 'list': 0.32; 'to:addr:python-list': 0.34; 'instead': 0.34; 'there': 0.34; 'duplicate': 0.35; 'identical': 0.35; 'actual': 0.35; 'some': 0.37; 'doing': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'subject:: ': 0.38; 'created': 0.38; 'think': 0.38; 'received:74.125.82': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.40; 'called': 0.40; 'unique': 0.64; '31,': 0.64; 'content.': 0.71; 'database.': 0.74; 'lose': 0.82; 'shoved': 0.84; 'subject:able': 0.84; 'subject:store': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=b2xZeOhIiTBhWwjbK+PrnQVK5Was4jRP/L616VKYtzY=; b=RTa/U+kuuKrFLnTBn/OoY4oazGJC2DeZrGNQxFPyi52AH+S3rJmoWDGLXQNL96K/tI cG47E1n9Sz0GG9uo8b42WqkhVQ/6RaShGxpkTbtkK3C+51TBMANfelDuxgPRY85poz2p bLyWCazkpHZclv8mhDeFiaFx7eGh24AEehJfw= MIME-Version: 1.0 In-Reply-To: References: Date: Sun, 31 Jul 2011 09:46:55 +0100 Subject: Re: Not able to store data to dictionary because of memory limitation From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 16 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312102017 news.xs4all.nl 23977 [2001:888:2000:d::a6]:48181 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10622 On Sun, Jul 31, 2011 at 9:38 AM, Rama Rao Polneni wrote: > Earlier I had many duplicate strings in different rows retrieved from database. > I created a list to have unique strings and their indexes are used in > actual computations. So lot of space is saved by having integers > instead of strings. > What you're doing there is called "interning" the strings, and I think it's the right thing to do. You can get the Python interpreter to do this for you by simply passing the strings through the built-in function intern() which will return a string with identical content. (In Python 3, that function has been shoved off to a module, so it's sys.intern() and you need to import sys.) You'll save some space and improve dictionary performance, but you won't lose clarity. Chris Angelico