Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'discard': 0.05; 'exist,': 0.07; 'python': 0.09; 'cc:addr:python-list': 0.10; 'times,': 0.13; 'formerly': 0.16; 'iterable': 0.16; 'iterable,': 0.16; 'mylist': 0.16; 'subject:between': 0.16; 'trivially': 0.16; 'xrange': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'example.': 0.17; 'versions': 0.20; '2.x': 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'possibly': 0.27; '3.0': 0.27; "doesn't": 0.28; 'fixed': 0.28; 'loop,': 0.29; 'convert': 0.29; 'usually': 0.30; 'asking': 0.32; 'space,': 0.32; 'form.': 0.33; 'times.': 0.33; 'list': 0.35; 'same.': 0.35; 'sometimes': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'takes': 0.39; 'received:192': 0.39; 'list,': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'range': 0.60; 'between': 0.63; 'more': 0.63; 'behavior': 0.64; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'presumably': 0.84 Date: Mon, 05 Nov 2012 09:36:41 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: inshu chauhan Subject: Re: Difference between range and xrange ?? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:UjgYlYwsgoEAP8hZeG/2kJgjurYCDxv6arRsOUJXooy cKkp4u4uUJ0Y9FxtU9gSECfX55aLLp+MqhUKTwcbL7VmIcvqYx kU+Nl6Hvz4hGUgg1lDtn/p4LSoSKikfsMGSYj0zWwZUTJmxydm 5T4xbER4yjRrwu6GiCDbUZHpE9vw/eity4AZIi8Itfcca2117h CaMttWPpTOjE0SFPRWHJqifeR9fhmgJ6deEuO/5FZLtZGRqjXH Y8fCGPRvaXu5hHLq0hI5Ae+W7wVBHEFaVH4vAzJzX7zJr/c1iI N2UjCe264MRlYp+oVEN4MolPE1E08MrAafK3OTUF95dx+XyQg= = Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352126226 news.xs4all.nl 6879 [2001:888:2000:d::a6]:39105 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32771 On 11/05/2012 09:23 AM, inshu chauhan wrote: > what is the difference between range and xrange.. both seem to work the > same. ? And which should be used where and in what situations.. ?? > > One difference is that from versions of Python 3.0 and later, xrange doesn't exist, and range takes over the behavior of what was formerly xrange. So presumably you're asking about Python 2.x In Python 2.x, range() generates a list, possibly a very large one. Sometimes that's exactly what you need. But other times, you're just using the list as an iterable, perhaps as a counter, or simply as a way to make a loop go a fixed number of times. xrange(), usually more efficient for speed, and certainly for space, generates an iterable. So it's interchangeable in a for loop, for example. In general, if you're going to discard the list immediately after using it, you should be using the iterable form, not the list form. In Python 3.x, if you really need a list, you can trivially convert an iterable into a list with the list "function." mylist = list(range(4)) -- DaveA