Problem with lists.

S

ssd

Hi,

In the following code, (in Python 2.5)
I was expecting to get in "b" variable the values b: [[0, 0], [0, 1],[0,
2], [0, 3],[0, 4], [1, 0],[1, 1], [1, 2], .....]
But I get only the last value [4,4], b: b: [[4, 4], [4, 4], [4, 4], ... ]

My code:

a = ["",""]
b = []

for i in range (0,5):
for j in range (0,5):
a[0] = i
a[1] = j
print "a: " + str(a)
b.append(a)

print "b: " + str(b)

what is worng in the code?

Thanks,
Bye,
 
D

desas2

Hi,

In the following code, (in Python 2.5)
I was expecting to get in "b" variable the values  b: [[0, 0], [0, 1],[0,
2], [0, 3],[0, 4], [1, 0],[1, 1], [1, 2], .....]
But I get only the last value [4,4], b: b: [[4, 4], [4, 4], [4, 4], ... ]

My code:

a = ["",""]
b = []

for i in range (0,5):
    for j in range (0,5):
        a[0] = i
        a[1] = j
        print "a: " + str(a)
        b.append(a)

print "b: " + str(b)

what is worng in the code?

Thanks,
Bye,

This is what I did and gives your expected results. I am sure, there
may be better way of doing it.
a = []
b =[]
for i in range(0,5):
for j in range(0,5):
a.append(i)
a.append(j)
b.append(a)
a =[]
print b

output:
[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1], [1, 2], [1,
3], [1, 4],
[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2], [3,
3], [3, 4],
[4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]

HTH
Dinakar
 
C

Chris Rebert

Hi,

In the following code, (in Python 2.5)
I was expecting to get in "b" variable the values b: [[0, 0], [0, 1],[0,
2], [0, 3],[0, 4], [1, 0],[1, 1], [1, 2], .....]
But I get only the last value [4,4], b: b: [[4, 4], [4, 4], [4, 4], ... ]

My code:

a = ["",""]
b = []

for i in range (0,5):
for j in range (0,5):
a[0] = i
a[1] = j
print "a: " + str(a)
b.append(a)

..append() does NOT copy the list `a`, it appends a /reference/ to `a`
(so to speak); thus, when you change `a` on the next iteration, you
change the same list *all* the entries in `b` point to.

Here's a corrected version:

b =[]
for i in range(0,5):
for j in range(0,5):
a = [i, j]
b.append(a)
print b

Cheers,
Chris
 
M

Matimus

Hi,

In the following code, (in Python 2.5)
I was expecting to get in "b" variable the values  b: [[0, 0], [0, 1],[0,
2], [0, 3],[0, 4], [1, 0],[1, 1], [1, 2], .....]
But I get only the last value [4,4], b: b: [[4, 4], [4, 4], [4, 4], ... ]

My code:

a = ["",""]
b = []

for i in range (0,5):
    for j in range (0,5):
        a[0] = i
        a[1] = j
        print "a: " + str(a)
        b.append(a)

print "b: " + str(b)

what is worng in the code?

Thanks,
Bye,

The problem is that `a' is the name of a list object. When you change
the contents of that list object you are simply mutating that object.
You then append that list object to list `b'. But, you aren't creating
a new list, you are simply mutating the same list object over and over
and appending it to list `b'. So list `b' contains 5 references to the
same object.

A couple of things I would do differently. There is no reason to
declare `a' outside of the loop. This isn't C you don't have to
declare your variables before they are needed. `range(0, 5)' is
usually just written `range(5)'. Also, check out `xrange'.


Here are a couple of examples of alternatives:

construct new lists and append them in the inner loop:

b = []
for i in range(5):
for j in range(5):
b.append([i, j])
print b


check out list comprehension:

b = [[i, j] for i in range(5) for j in range(5)]
print b


Matt
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top