Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsfeed.xs4all.nl!newsfeed2.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'duplicate': 0.07; 'output,': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'operation.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'later': 0.20; 'header:User-Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'help!': 0.26; 'received:(smtp server)': 0.26; 'this:': 0.26; 'code:': 0.26; 'header:In-Reply- To:1': 0.27; 'statement': 0.30; 'go.': 0.31; 'values.': 0.31; 'but': 0.35; 'building': 0.35; 'subject:List': 0.36; 'doing': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'different': 0.65; 'therefore': 0.72 X-Virus-Scanned: OK Date: Wed, 08 May 2013 23:54:24 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130404 Thunderbird/17.0.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Append to python List References: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> In-Reply-To: <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368082909 news.xs4all.nl 15900 [2001:888:2000:d::a6]:40131 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45013 On 05/08/2013 11:36 PM, RAHUL RAJ wrote: > Checkout the following code: > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > output=[] > output=[x for x in sample2 if x not in output] This statement is not doing what you expect. It is not building a list in the variable named output, it is building a list (anonymously) then binding it to the variable output once it's built. Therefore output is [] for the whole list building operation. The later operation works, because your *are* building the list in place as you go. > > the output I get is > 3 4 5 6 7 8 9 10 3 5 6 7 8 9 10 11 4 5 7 8 9 10 11 12 5 6 7 9 10 11 12 13 6 7 8 9 11 12 13 14 7 8 9 10 11 13 14 15 8 9 10 11 12 13 15 16 9 10 11 12 13 14 15 17 10 11 12 13 14 15 16 17 > > which contains duplicate values. > > > > > But if I do like this: > > sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y] > output=[] > for x in sample2: > if x not in output: > output.append(x) > > > the value of 'output' I get like this: > 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 > > I know that both the programs have the same functionality, but why do I have different outputs? > > Please help!