Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:error': 0.03; 'element': 0.07; 'elements.': 0.07; 'memory.': 0.07; 'compact': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'jan': 0.12; '23,': 0.16; 'bounds': 0.16; 'btw': 0.16; 'construct.': 0.16; 'desirable,': 0.16; 'once.': 0.16; 'range.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'versatile': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'slightly': 0.19; 'thu,': 0.19; 'version.': 0.19; '>>>': 0.22; 'memory': 0.22; 'example': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'related': 0.29; "doesn't": 0.30; 'forgot': 0.30; '(which': 0.31; 'membership': 0.31; 'forces': 0.31; 'testing.': 0.31; 'allows': 0.31; 'common': 0.35; 'knows': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'are,': 0.36; 'false': 0.36; 'sequence': 0.36; 'useful': 0.36; 'too': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'fact': 0.38; 'pm,': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'ian': 0.60; 'improved': 0.60; 'most': 0.60; 'entire': 0.61; 'range': 0.61; 'simple': 0.61; 'more': 0.64; 'needing': 0.65; 'specialized': 0.65; 'containing': 0.69; 'ranges': 0.74; 'excessive': 0.84; 'received:fios.verizon.net': 0.84; 'discovering': 0.91; 'subject:Lists': 0.91; 'insane': 0.93; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: I am out of trial and error again Lists Date: Thu, 23 Oct 2014 23:14:37 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414120492 news.xs4all.nl 2890 [2001:888:2000:d::a6]:39333 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79555 On 10/23/2014 3:39 PM, Ian Kelly wrote: > On Thu, Oct 23, 2014 at 11:07 AM, Seymore4Head > wrote: >> BTW I forgot to add that example 2 and 3 don't seem to be too useful >> in Python 3, but they are in Python 2. I don't understand how the >> Python 3 is an improved version. > > In Python 2, range returns a list containing all the requested > elements. This is simple to work with but often not desirable, because > it forces the entire sequence to be loaded into memory at once. Large > ranges may cause excessive memory use and related slowdowns, while > very large ranges may not even be usable. > > In Python 3, range returns a compact object that knows what elements > it contains and can iterate over them (which is the most common use of > ranges by far), but without needing to load them all into memory at > once. While iterating, only the current element needs to exist in > memory. You can still get the range as a list if you want it, by > calling list(range(10)), but it doesn't force you to create a list, > which makes it more versatile than the Python 2 construct. > > The more specialized data structure used in Python 3 also allows for > certain optimizations, for example membership testing. In Python 2, if > you do the test "97 in range(100)", it has to construct a 100-element > list and then iterate over 97 of the elements before discovering that > the list does in fact contain the number 97. The Python 3 range object > knows what the bounds of the range are, so all it has to do is check > that 0 <= 97 < 100 to know that the number 97 is included in the > range. Example that works 'instantly' in 3.x, but would be slightly insane in 2.x, even if you have enough memory for it to work on a 64 bit build. >>> r =range(0, 1000000000000000, 999937) >>> 3428761974385 in r False >>> 3428761974386 in r True -- Terry Jan Reedy