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


Groups > comp.lang.python > #45009

Re: Append to python List

From Jussi Piitulainen <jpiitula@ling.helsinki.fi>
Newsgroups comp.lang.python
Subject Re: Append to python List
Date 2013-05-09 09:55 +0300
Organization University of Helsinki
Message-ID <qotobck8wx3.fsf@ruuvi.it.helsinki.fi> (permalink)
References <71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com>

Show all headers | View raw


RAHUL RAJ writes:

> 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]
> 
> 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.

The second comprehension, [x for x in sample2 if x not in output], in
the context, is equivalent to [x for x in sample2 if x not in []]. It
does not refer to an incomplete version of the list that gets assigned
to the variable after it's done.

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