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


Groups > comp.lang.python > #45007 > unrolled thread

Append to python List

Started byRAHUL RAJ <omrahulrajcse@gmail.com>
First post2013-05-08 23:36 -0700
Last post2013-05-09 11:14 +0000
Articles 16 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#45007 — Append to python List

FromRAHUL RAJ <omrahulrajcse@gmail.com>
Date2013-05-08 23:36 -0700
SubjectAppend to python List
Message-ID<71427882-d2c6-4066-b1e2-624b12a42a0d@googlegroups.com>
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.




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!

[toc] | [next] | [standalone]


#45009

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-05-09 09:55 +0300
Message-ID<qotobck8wx3.fsf@ruuvi.it.helsinki.fi>
In reply to#45007
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.

[toc] | [prev] | [next] | [standalone]


#45025

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-05-09 04:24 -0700
Message-ID<11dfb7f9-9bbb-4e42-bce9-027efffabcf3@googlegroups.com>
In reply to#45009
Jussi Piitulainen於 2013年5月9日星期四UTC+8下午2時55分20秒寫道:
> 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.

This is just the handy style for a non-critical loop.
In a critical loop, the number of  the total operation counts
does matter in the execution speed.

[toc] | [prev] | [next] | [standalone]


#45026

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-05-09 14:30 +0300
Message-ID<qottxmccrwi.fsf@ruuvi.it.helsinki.fi>
In reply to#45025
88888 Dihedral writes:

> This is just the handy style for a non-critical loop.
> In a critical loop, the number of  the total operation counts
> does matter in the execution speed.

Do you use speed often?

[toc] | [prev] | [next] | [standalone]


#45091

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-05-10 02:51 -0700
Message-ID<34c6abdd-91f3-4c24-bb82-bd5063902bd0@googlegroups.com>
In reply to#45026
Jussi Piitulainen於 2013年5月9日星期四UTC+8下午7時30分05秒寫道:
> 88888 Dihedral writes:
> 
> 
> 
> > This is just the handy style for a non-critical loop.
> 
> > In a critical loop, the number of  the total operation counts
> 
> > does matter in the execution speed.


> 
> 
> 
> Do you use speed often?

There is another concern about the list construction part
in programming.

Although a typical PC is installed with gaga bytes of DRAM
now,  anything that will use more memory from the heap
dynamically could fail in the run time.

It is the programmer's job to identify this kind of sources
in minds.

[toc] | [prev] | [next] | [standalone]


#45132

FromChris Angelico <rosuav@gmail.com>
Date2013-05-09 21:49 +1000
Message-ID<mailman.1551.1368273954.3114.python-list@python.org>
In reply to#45026
On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> wrote:
> 88888 Dihedral writes:
>
>> This is just the handy style for a non-critical loop.
>> In a critical loop, the number of  the total operation counts
>> does matter in the execution speed.
>
> Do you use speed often?

Dihedral is a bot. Quite a good one, but a bot.

ChrisA

[toc] | [prev] | [next] | [standalone]


#45142

FromAnssi Saari <as@sci.fi>
Date2013-05-11 18:47 +0300
Message-ID<vg3ip2po6wp.fsf@coffee.modeemi.fi>
In reply to#45132
Chris Angelico <rosuav@gmail.com> writes:

> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen
> <jpiitula@ling.helsinki.fi> wrote:
>> 88888 Dihedral writes:
>>
>>> This is just the handy style for a non-critical loop.
>>> In a critical loop, the number of  the total operation counts
>>> does matter in the execution speed.
>>
>> Do you use speed often?
>
> Dihedral is a bot. Quite a good one, but a bot.

That's been said often enough. Is the source available and is it in
Python?

[toc] | [prev] | [next] | [standalone]


#45143

FromChris Angelico <rosuav@gmail.com>
Date2013-05-12 02:00 +1000
Message-ID<mailman.1558.1368288047.3114.python-list@python.org>
In reply to#45142
On Sun, May 12, 2013 at 1:47 AM, Anssi Saari <as@sci.fi> wrote:
> Chris Angelico <rosuav@gmail.com> writes:
>
>> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen
>> <jpiitula@ling.helsinki.fi> wrote:
>>> 88888 Dihedral writes:
>>>
>>>> This is just the handy style for a non-critical loop.
>>>> In a critical loop, the number of  the total operation counts
>>>> does matter in the execution speed.
>>>
>>> Do you use speed often?
>>
>> Dihedral is a bot. Quite a good one, but a bot.
>
> That's been said often enough. Is the source available and is it in
> Python?

Not to my knowledge. Technically Dihedral is merely _rumoured_ to be a
bot, as we have no actual proof; but we've been conducting a variety
of Turing tests via this list and have yet to see any strong argument
for his being deemed human. Most humans would get defensive, or at
least protest, if treated as bots; Dihedral never has, despite being
referred to in this way a number of times.

ChrisA

[toc] | [prev] | [next] | [standalone]


#45170

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-05-11 19:29 -0700
Message-ID<458e28f3-56ad-4fea-87ec-1f1858f5d414@googlegroups.com>
In reply to#45143
Chris Angelico於 2013年5月12日星期日UTC+8上午12時00分44秒寫道:
> On Sun, May 12, 2013 at 1:47 AM, Anssi Saari <as@sci.fi> wrote:
> 
> > Chris Angelico <rosuav@gmail.com> writes:
> 
> >
> 
> >> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen
> 
> >> <jpiitula@ling.helsinki.fi> wrote:
> 
> >>> 88888 Dihedral writes:
> 
> >>>
> 
> >>>> This is just the handy style for a non-critical loop.
> 
> >>>> In a critical loop, the number of  the total operation counts
> 
> >>>> does matter in the execution speed.
> 
> >>>
> 
> >>> Do you use speed often?
> 
> >>
> 
> >> Dihedral is a bot. Quite a good one, but a bot.
> 
> >
> 
> > That's been said often enough. Is the source available and is it in
> 
> > Python?
> 
> 
> 
> Not to my knowledge. Technically Dihedral is merely _rumoured_ to be a
> 
> bot, as we have no actual proof; but we've been conducting a variety
> 
> of Turing tests via this list and have yet to see any strong argument
> 
> for his being deemed human. Most humans would get defensive, or at
> 
> least protest, if treated as bots; Dihedral never has, despite being
> 
> referred to in this way a number of times.
> 
> 
> 
> ChrisA

Don't you get the practices of   POSIX ?

[toc] | [prev] | [next] | [standalone]


#45171

FromChris Angelico <rosuav@gmail.com>
Date2013-05-12 12:40 +1000
Message-ID<mailman.1575.1368326441.3114.python-list@python.org>
In reply to#45170
On Sun, May 12, 2013 at 12:29 PM, 88888 Dihedral
<dihedral88888@googlemail.com> wrote:
> Chris Angelico於 2013年5月12日星期日UTC+8上午12時00分44秒寫道:
>> Most humans would get defensive, or at
>> least protest, if treated as bots; Dihedral never has, despite being
>> referred to in this way a number of times.
>>
>> ChrisA
>
> Don't you get the practices of   POSIX ?

I rest my case, m'lud.

ChrisA

[toc] | [prev] | [next] | [standalone]


#45178

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-05-12 09:02 +0300
Message-ID<qot8v3kloq6.fsf@ruuvi.it.helsinki.fi>
In reply to#45132
Chris Angelico writes:
> On Thu, May 9, 2013 at 9:30 PM, Jussi Piitulainen wrote:
> > 88888 Dihedral writes:
> >
> >> This is just the handy style for a non-critical loop.
> >> In a critical loop, the number of  the total operation counts
> >> does matter in the execution speed.
> >
> > Do you use speed often?
> 
> Dihedral is a bot. Quite a good one, but a bot.

Yes, I understood why people say so when it followed up to something I
wrote myself, and what it wrote made no sense in the context. My
response was also generated by a bot: M-x doctor in Emacs, meant to be
funny.

Don't worry, I'm not going to engage it further.

[toc] | [prev] | [next] | [standalone]


#45012

FromChris Angelico <rosuav@gmail.com>
Date2013-05-09 16:52 +1000
Message-ID<mailman.1484.1368082844.3114.python-list@python.org>
In reply to#45007
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

[toc] | [prev] | [next] | [standalone]


#45013

FromGary Herron <gary.herron@islandtraining.com>
Date2013-05-08 23:54 -0700
Message-ID<mailman.1485.1368082909.3114.python-list@python.org>
In reply to#45007
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!

[toc] | [prev] | [next] | [standalone]


#45016

FromRAHUL RAJ <omrahulrajcse@gmail.com>
Date2013-05-09 01:18 -0700
Message-ID<b8608b36-95f6-40df-a172-b179cb5fd8ee@googlegroups.com>
In reply to#45013
Then what about this code part?

[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]

and the following code part:

for x in [1,2,3]:
  for y in [3,1,4]:
    if x != y:
      combs.append((x, y))


On Thursday, May 9, 2013 12:24:24 PM UTC+5:30, Gary Herron wrote:
> 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!

[toc] | [prev] | [next] | [standalone]


#45017

FromRAHUL RAJ <omrahulrajcse@gmail.com>
Date2013-05-09 01:19 -0700
Message-ID<51959fcc-8653-47c0-bc46-b30e82e40f35@googlegroups.com>
In reply to#45016
I'm getting same output for both code parts, why not for th code parts in question?

On Thursday, May 9, 2013 1:48:51 PM UTC+5:30, RAHUL RAJ wrote:
> Then what about this code part?
> 
> 
> 
> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
> 
> 
> 
> and the following code part:
> 
> 
> 
> for x in [1,2,3]:
> 
>   for y in [3,1,4]:
> 
>     if x != y:
> 
>       combs.append((x, y))
> 
> 
> 
> 
> 
> On Thursday, May 9, 2013 12:24:24 PM UTC+5:30, Gary Herron wrote:
> 
> > 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!

[toc] | [prev] | [next] | [standalone]


#45023

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-09 11:14 +0000
Message-ID<518b8527$0$29997$c3e8da3$5496439d@news.astraweb.com>
In reply to#45016
On Thu, 09 May 2013 01:18:51 -0700, RAHUL RAJ wrote:

> Then what about this code part?


What about it? 


> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
> 
> and the following code part:
> 
> for x in [1,2,3]:
>   for y in [3,1,4]:
>     if x != y:
>       combs.append((x, y))

Apart from not defined combs, those two pieces of code are equivalent.

So what is your question?


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web