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


Groups > comp.lang.python > #45012

Re: Append to python List

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.069
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; '16,': 0.03; 'constructs': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'set,': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'instead.': 0.24; '15,': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'points': 0.29; 'message- id:@mail.gmail.com': 0.30; '13,': 0.31; 'skip:_ 10': 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'subject:List': 0.36; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'name': 0.63; 'more': 0.64; 'received:209.85.220.44': 0.84; 'received:mail-pa0-f44.google.com': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=q7rfVbPsloudcJBt0yWVLlgeUuxcOaLG5FufMaJsgqs=; b=eRiIRhF58syW6hXKJGOYFg9BGN+GP0cDKLKQDGq9++2sDoKiB0IqBKmKEJmGyb20YT PREfd/IU3s4Abcec491RYroOq6NUF295u4AcoLw29zuEn+ubasldRDxoInX12uN7e2y/ NFiJtos3yDPPlN22xFT22IeI2Kd4rTCEQ4mEPqwz7S1V1IylgfZqs0PLPCYxNo7ORrKB cteZOPdR0SLQ4AZJkqh1DrjEdLKUqfEH1LMVvBxYp6AaONymtIeHb+3imOp5HOSrEvNE nLQgWSvDJQ6L/FQj2SSrAN5H0bQwHEi5rza18RMGg/cJTT1rgcVmEEo4JwDaPcnpx0fa UruQ==
MIME-Version 1.0
X-Received by 10.66.50.4 with SMTP id y4mr11760247pan.216.1368082362730; Wed, 08 May 2013 23:52:42 -0700 (PDT)
In-Reply-To <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com>
References <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com>
Date Thu, 9 May 2013 16:52:42 +1000
Subject Re: Append to python List
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1484.1368082844.3114.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368082844 news.xs4all.nl 15983 [2001:888:2000:d::a6]:38722
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45012

Show key headers only | View raw


On Thu, May 9, 2013 at 4:36 PM, RAHUL RAJ <omrahulrajcse@gmail.com> wrote:
> output=[x for x in sample2 if x not in output]
>
> output=[]
> for x in sample2:
>   if x not in output:
>      output.append(x)

The first one constructs a list, then points the name 'output' at it.
The second one builds up a list, with 'output' pointing at it all the
way. Your first one is more like:

sample2 = [x+y for x in range(1,10) for y in range(1,10) if x!=y]
output=[]
_temp=[]
for x in sample2:
  if x not in output:
     _temp.append(x)
output=_temp

You may want to consider using a set, instead.

>>> {x+y for x in range(1,10) for y in range(1,10) if x!=y}
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Append to python List RAHUL RAJ <omrahulrajcse@gmail.com> - 2013-05-08 23:36 -0700
  Re: Append to python List Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-05-09 09:55 +0300
    Re: Append to python List 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-09 04:24 -0700
      Re: Append to python List Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-05-09 14:30 +0300
        Re: Append to python List 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-10 02:51 -0700
        Re: Append to python List Chris Angelico <rosuav@gmail.com> - 2013-05-09 21:49 +1000
          Re: Append to python List Anssi Saari <as@sci.fi> - 2013-05-11 18:47 +0300
            Re: Append to python List Chris Angelico <rosuav@gmail.com> - 2013-05-12 02:00 +1000
              Re: Append to python List 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-11 19:29 -0700
                Re: Append to python List Chris Angelico <rosuav@gmail.com> - 2013-05-12 12:40 +1000
          Re: Append to python List Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-05-12 09:02 +0300
  Re: Append to python List Chris Angelico <rosuav@gmail.com> - 2013-05-09 16:52 +1000
  Re: Append to python List Gary Herron <gary.herron@islandtraining.com> - 2013-05-08 23:54 -0700
    Re: Append to python List RAHUL RAJ <omrahulrajcse@gmail.com> - 2013-05-09 01:18 -0700
      Re: Append to python List RAHUL RAJ <omrahulrajcse@gmail.com> - 2013-05-09 01:19 -0700
      Re: Append to python List Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-09 11:14 +0000

csiph-web