Append to python List

R

RAHUL RAJ

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!
 
C

Chris Angelico

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.
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}

ChrisA
 
G

Gary Herron

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!
 
J

Jussi Piitulainen

RAHUL said:
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.
 
R

RAHUL RAJ

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))


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:



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!
 
R

RAHUL RAJ

I'm getting same output for both code parts, why not for th code parts in question?

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 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=[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]
for x in sample2:
if x not in output:
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!
 
S

Steven D'Aprano

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?
 
8

88888 Dihedral

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

Jussi Piitulainen

88888 said:
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?
 
8

88888 Dihedral

Jussi Piitulainenæ–¼ 2013å¹´5月9日星期四UTC+8下åˆ7時30分05秒寫é“:
88888 Dihedral writes:









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

Chris Angelico

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
 
8

88888 Dihedral

Chris Angelicoæ–¼ 2013å¹´5月12日星期日UTC+8上åˆ12時00分44秒寫é“:
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 ?
 
C

Chris Angelico

Chris Angelico©ó 2013¦~5¤ë12¤é¬P´Á¤éUTC+8¤W¤È12®É00¤À44¬í¼g¹D¡G

Don't you get the practices of POSIX ?

I rest my case, m'lud.

ChrisA
 
J

Jussi Piitulainen

Chris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top