I have been working with lists for a year now, but I am inconsistent with the way I have them setup. I want to know the standard. Let's say you have a computer represented as pixel colors in the form of a list. To simplify it let's just use numbers 1 through 9. Top left corner 1, top right corner 3, bottom left 7, bottom right 9, and the middle is 5. Which of the two ways below is the proper way to convert the screen into a list. The first way is visually appealing when you see it in the code, but accessing it this way is counterintuitive since I use [y][x] rather than [x][y]. The second way, the list in the code looks a little bit unfamiliar to what it actually represents, but it can be accessed intuitively by [x][y], and it is looped with x first. So which way is the standard or most common way of arranging lists in this situation?

a = [[1,2,3],
     [4,5,6],
     [7,8,9]]

Accessed by: a[y][x]
Loops with:

for y in range(len(a)):
    for x in range(len(a[y])):
        a[y][x]

or

a = [[1,4,7],
     [2,5,8],
     [3,6,9]]

Accessed by: a[x][y]
Loops with:

for x in range(len(a)):
    for y in range(len(a[y])):
        a[x][y]

I would say it's a[row][column] . So, use 'row' and 'col' instead of x and y.

I would say it's a[row][column] . So, use 'row' and 'col' instead of x and y.

a = [[1,2,3],
     [4,5,6],
     [7,8,9]]
a = [[1,4,7],
     [2,5,8],
     [3,6,9]]

Okay, now which way should I organize the list? The first way or the second way? PRobably the first way if I'm changing to col and row.

a = [[1,2,3],
     [4,5,6],
     [7,8,9]]
a = [[1,4,7],
     [2,5,8],
     [3,6,9]]

Okay, now which way should I organize the list? The first way or the second way? PRobably the first way if I'm changing to col and row.

There is no rule, think about your list as a table, it all depends on the semantics of your rows and colums. For example if your table contains students marks, the logical way to organize the table would be that each item of the list contains all the marks for a given student, so there would be a row for each student with the list of his/her marks. That's the way tables are designed in relational databases.

should be the first one.

but it does not really matters. It all depends on your data.

In fact it depends on the use as both richieking and Gribouillis suggest. The technical terms are 'row major' (indexed as data[row][column] ) and 'column major' (indexed as data[column][row] ). Some math applications work much better column major, for instance. For "large" data, it can matter (perhaps not so much in Python) what order the data is usually accessed so as to avoid memory cache misses: Things accessed close in time should be stored nearby in memory.

I like using dictionaries for my multidimensional containers using coordinate tuples as keys. It's not a conventional approach, but despite any disadvantages it has some advantages. For very small or very large coordinates, it has a memory advantage because "coordinates" don't need to exist until they are assigned to. The fact that coordinates can be any hashable objects also presents some interesting options.

commented: A nice bright light went off over my head. Thanks. +1

I like using dictionaries for my multidimensional containers using coordinate tuples as keys. It's not a conventional approach, but despite any disadvantages it has some advantages. For very small or very large coordinates, it has a memory advantage because "coordinates" don't need to exist until they are assigned to. The fact that coordinates can be any hashable objects also presents some interesting options.

Interesting. I had not considered that a sparse array was so, well, easy in Python. Thank you for that insight.

A dictionary coordinate tuple still has the row column problem.
There is a code snippet about that here on DaniWeb.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.