Mailing List Archive

A simple question
I know the answer is probably really simple to this, and I feel bad to
even ask, but I can't find the answer anywhere... Let me show what
happened, then ask the question.

>>> x=[[0]*2]*2
>>> x
[[0, 0], [0, 0]]
>>> x[0][1]=1
>>> x
[[0, 1], [0, 1]]
>>>

The question now. Why is the output list [[0, 1], [0, 1]] and not [[0,
1], [0, 0]]? And how can I make it work right? I do know that x[0][1]
will always give the value of that specific coordinate, but never
before have I tried to manipulate rows like this, and I'm finding I
need to. Thanks!

--
http://mail.python.org/mailman/listinfo/python-list
Re: A simple question [ In reply to ]
>>> x=[[0]*2]*2

This replicates the references. It doesn't copy objects.

This short transcript demonstrates that concept:

>>> x = [[0, 0], [0, 0]]
>>> map(id, x)
[16988720, 16988160]
>>> y = [[0]*2]*2
>>> y
[[0, 0], [0, 0]]
>>> map(id, y)
[16988520, 16988520]

The object x refers to is a list with references to two other lists. The
object y refers to is a list with two references to the same list.

Skip
--
http://mail.python.org/mailman/listinfo/python-list
Re: A simple question [ In reply to ]
Skip answered why, but not how to make it work right:

>>> x = [[0]*2 for x in range(2)]
>>> x
[[0, 0], [0, 0]]
>>> x[0][1]=1
>>> x
[[0, 1], [0, 0]]

Cheers,
n

--
http://mail.python.org/mailman/listinfo/python-list
Re: A simple question [ In reply to ]
Tuvas wrote:
> Why is the output list [[0, 1], [0, 1]] and not [[0,
> 1], [0, 0]]? And how can I make it work right?

http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list

--Ben

--
http://mail.python.org/mailman/listinfo/python-list
Re: A simple question [ In reply to ]
Ahh, that make sense! Thanks a ton!

--
http://mail.python.org/mailman/listinfo/python-list
RE: a simple question [ In reply to ]
In addition to the other requests for more info on a very ambiguous
question, let me add one. Did the message suggest something worked on an
earlier version and broke with a new version, or did they mean they just
started USING the current version and hoped the version number was
meaningful for the way they probably did not write proper code in the first
place that loads the module(s) they need?

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On
Behalf Of Dennis Lee Bieber
Sent: Monday, July 26, 2021 9:46 PM
To: python-list@python.org
Subject: Re: a simple question

On Mon, 26 Jul 2021 22:19:58 +0000 (UTC), Glenn Wilson via Python-list
<python-list@python.org> declaimed the following:

>I recently downloaded the latest version of python, 3.9.6. Everything works
except, the turtle module. I get an error message every time , I use basic
commands like forward, backward, right and left. My syntax is correct:
pat.forward(100) is an example. Can you tell me what is wrong.

No we can't.

Please provide a cut&paste (NOT a screen-grab image) of the error
traceback, an indication of what OS/hardware you are running upon, etc.
Maybe even which installer you downloaded (python.org, or some third-party
repackager: ActiveState, Anaconda, M$ app store)



--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: a simple question [ In reply to ]
On Tuesday, July 27, 2021 at 5:05:27 AM UTC-7, Terry Reedy wrote:
> On 7/26/2021 6:19 PM, Glenn Wilson via Python-list wrote:
> > I recently downloaded the latest version of python, 3.9.6. Everything works except, the turtle module. I get an error message every time , I use basic commands like forward, backward, right and left. My syntax is correct: pat.forward(100) is an example. Can you tell me what is wrong.
> On Windows, normal install,
> C:\Users\Terry>py -3.9 -m turtle
> C:\Users\Terry>py -3.9 -m turtledemo
> both work.
>
>
> --
> Terry Jan Reedy

Welcome to programming!! Ain't it fun?!
--
https://mail.python.org/mailman/listinfo/python-list