[Tutor] Two dimensional lists

Cameron Simpson cs at cskk.id.au
Mon Nov 2 02:22:01 EST 2020


On 02Nov2020 16:25, Phil <phillor9 at gmail.com> wrote:
>On 2/11/20 2:46 pm, Cameron Simpson wrote:
>Thank you Cameron for your insightful reply.

Remarks below.

>For the syntax you show above, the direct Python equivalent is a 5
>>element list, each element of which is a 5 element list.
>>
>>So:
>>
>>     b = [
>>           [ 0, 0, 0, 0, 0 ],
>>           [ 0, 0, 0, 0, 0 ],
>>           [ 0, 0, 0, 0, 0 ],
>>           [ 0, 0, 0, 0, 0 ],
>>           [ 0, 0, 0, 0, 0 ],
>>         ]
>>
>>The syntax you use "b[3][2]" fetches element 3 (the fourth element,
>>since lists count from 0), and then fetches element 2 from _that_. So
>>the third element of the fourth list.
>
>That's what I expected as well, however, this is the result;
>
>>>> b = [5],[5]
>>>> b[3][2] = 9
>Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>IndexError: tuple index out of range
>>>> print(b[3][2])
>Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>IndexError: tuple index out of range

But what you wrote is not what I wrote.

I've explicitly laid out a list of lists.

You've laid out a 2-tuple of 1 element lists:

    b = [5],[5]

Perhaps I should note here that you don't "declare" Python variables, so 
the above is not a declaration of some kind of array, it is an 
assignment of a little tuple, containing 2 lists.

The above may make more sense to you as this:

    b = ([5], [5])

Does that loook more obvious? Had I written it out as in my original 
example it would look like this:

    b = (
        [ 5 ],
        [ 5 ],
      )

If not, consider:

    x = [5]

makes a _single_ element list, whose sole element is the value 5. It 
does not create a 5 element list.

This:

    x = [1, 2]

makes a 2 element list, and this:

    x = (1, 2)

makes a 2 element tuple (effectively an immutable fixed length list, 
semanticly, but that immutability bring other features, irrelevant 
here).

Now, the brackets are not part of Python's tuple syntax - they're only 
needed for disambiguation. This still makes a 2 element tuple:

    x = 1, 2

Is it now evident why your:

    b = [5],[5]

does not make 2 dimensional list/array, it makes a little tuple of 
1-element lists?

>I'd spent the last 3 or 4 hours experimenting with this but have not 
>achieved anything useful. I also had a play with the array module and 
>I had another look at numpy and have decided that's the way to go.

For many purposes, that is probably true. Numpy is explicitly desogned 
to bring bulk numeric computation to Python, and that often involves 
matrices etc. The array module is probably less useful, but it is 
designed for compact allocation of bulk objects in memory, IIRC.

>Still, I'm a little disappointed that this has defeated me.

You're defeated because you're coming in reading:

    b = [5],[5]

as an array declaration, which it is not. Clear that away and things 
should make more sense.

Then come back when you're having trouble making the data structures you 
actually need, and we'll see how one would approach that in Python.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list