Accessing a List Variable


#1

Hi everyone,

Can someone help me with this?
If i have a list declared, for example n=[12,21,32,47,54] I have access to the third element of the list typing n[2]="something", however can i have access to the same element typing n[0][2]="something"?
Thank u for your atention


#2

Accessing means referring (as in querying by index) or mutating (as in modifying by index).

print n[2]         =>  querying
n[2] = some_value  =>  mutating

Either are in the above accessing a single element in a linear list. We can think of them as rows in an array. Then we can consider that each row contains another list. This list will have its own index just like the one above, except it will need to be referred by its row, first.

n = [
    [ 12, 21, 32, 47, 54 ],
    [ 17, 29, 35, 44, 58 ],
    [ 13, 25, 38, 49, 51 ]
]

This is where n[0][2] comes in. It will be 32. Row 1, column 3.