python-novice-gapminder icon indicating copy to clipboard operation
python-novice-gapminder copied to clipboard

list of lists are not two-dimensional

Open mhagdorn opened this issue 4 years ago • 3 comments

In the list episode in the section on appending items it says that appending a list to a list makes the result two-dimensional. Lists are always one-dimensional. Only arrays can be multi-dimensional. List of lists arise because a list can store any object (including a list). Sometimes lists of lists can be converted to an array if the length of the inner lists are the same.

I would probably say that lists are always one dimensional and refer to arrays for multi-dimensional data-structures.

mhagdorn avatar Aug 20 '20 11:08 mhagdorn

Well, it is a matter of interpretation. A nested list, e.g. a list of lists of numbers can be seen as a data representation of a matrix and you access individual elements with lst[i][j] which makes it two-dimensional in nature. So even a list as such is one-dimensional it can still represent something multi-dimensional.

vahtras avatar Aug 20 '20 13:08 vahtras

I prefer the stricter interpretation - the number of dimensions (and their size) is an intrinsic property of the object. I realise that you can use lists for all sorts of things but they are general purpose containers of any type of object. Arrays are natively multidimensional and are much more suitable to represent vectors and matrices.

I realise this is probably out of scope for this workshop, but maybe a reference to arrays?

Another option would be to move the section Lists may contain values of different types up and say that list can also include other lists.

mhagdorn avatar Aug 20 '20 14:08 mhagdorn

I think this could probably benefit from some clarification. Since Python does not have native support for arrays (besides library support e.g., numpy or https://docs.python.org/3/library/array.html) it's common to use list of lists as the next best substitute.

I'd be in favor of moving the lists can contain values of any type up and stating clearly that lists can contain arbitrary objects which include other collections: lists, dictionaries, etc.

alee avatar Aug 24 '20 23:08 alee

Discussion can be continued in PR #655.

martinosorb avatar Jul 24 '23 19:07 martinosorb