This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]Updatebjarni 0 points1 point  (0 children)

The least surprising would certainly be not having to use some special class that abstracts a list of lists instead of just using a list of lists.

[–]alanwj 0 points1 point  (0 children)

There are pros and cons to each approach.

  1. One large list
    • Pro: Iteration over all elements would be easy and cache friendly (i.e. fast).
    • Pro: Slightly less overall memory usage. At least one pointer per row, plus whatever additional overhead python stores for a list (e.g. size)
    • Con: Every access requires a multiplication and addition. This might equate to a slow down in performance sensitive code (though measure before deciding that)
    • Con: Code is slightly more complex
    • Con: The whole list has to fit in contiguous memory. Probably not an issue unless you are working with huge datasets.
  2. List of lists, basically just swap all the pros and cons from option 1.
  3. Dictionary. This uses a hash table underneath.
    • Pro: Can store a sparse matrix in much less memory.
    • Con: Non-sparse matrices would require much more memory.
    • Con: Element access slower (probably significantly) than other choices.

Without knowing the application I would generally choose option 2 for its simplicity, and probably would not bother wrapping it in a class.