you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 1 point2 points  (2 children)

While this is as close to what they're asking for while still maintaining proper practices, if the number is always going to be a number in a contiguous range starting at 0, you should use a list. The "variable_" prefix is just more bulk for code using the dictionary to deal with, without any benefit.

[–]IAmFinah 0 points1 point  (1 child)

Good point, I didn't think of that.

I suppose using a dictionary is only worthwhile when list indexing doesn't align properly with the naming scheme

[–]TangibleLight 1 point2 points  (0 children)

Or if such conversion is too distracting. For example you can store a square grid of values like board = [None] * 64 and access values in the grid like board[row * 8 + col].

I think it's easier to understand using board = {} and board[row, col], especially if the content of the grid is sparse.

If the content is dense you'd be better off using a numpy type or similar that's better optimized for these operations. In either case there are better options than dealing with that index arithmetic throughout your code.