[Tutor] modifying lists of lists

Steven D'Aprano steve at pearwood.info
Thu Oct 4 04:22:35 CEST 2012

On 04/10/12 11:46, Ed Owens wrote:
> I'm just learning Python, so I apologize for a newby question.  I'm trying
> to work with lists of lists, the lowest level of which hold one or more
> tuples.  I've tried to condense what I've tried.

Hi Ed, and welcome!

> The code is:

I'm afraid I can't make heads nor tails of *why* you are trying this. I can
guess you are testing something about copying lists, but comments like this:

> # H.append(tuple) did it three times for example, and because H=I=J won't
> work for the example.

leave me mystified. "did it three times for example"? Did what? What counts
as working?

You pepper your comments with things like "doesn't work", "can't do this",
"can't be edited" etc., but without knowing what you expect to happen it's
hard to understand what you mean.

How about if you start off with a simple example, and you show what happens
when you run the code, AND what you expected to happen?

To make it easy, here is my *guess* as to the sort of thing that is
confusing you.

py> H = [[1, 2]]
py> J = [H[0]]
py> print H
[[1, 2]]
py> print J
[[1, 2]]
py> H[0][0] = 99
py> print H  # expected, and got, [[99, 2]]
[[99, 2]]
py> print J  # expected [[1, 2]]
[[99, 2]]

Am I close? If I am, I (or one of the other tutors) will explain what
is going on. Otherwise, you will have to explain what you are trying
to do in more detail.


More information about the Tutor mailing list