Reputation: 231
Aside from the different accessing methods (e.g. [1,2]
instead of [1][2]
to access element in 2nd row and 3rd column), what are the differences between multidimensional arrays and nested lists in python? Why are both data structures necessary?
Upvotes: 3
Views: 2511
Reputation: 1123410
Python does not have a multidimensional array type. It only has lists.
numpy
(a 3rd-party Python extension) does have array types, and these serve a specialized function within that library, namely fast C-based mathematical operations on homogenous sequences.
With the standard Python list
type, putting one inside the other creates a nested structure that can be used to model a multidimensional structure. You nest the [index]
item access, [1][42]
first retrieves the second element of the outer list, then the 43rd element of that second element.
numpy
arrays are specialist structures that explicitly model multiple dimensions as part of the main type, rather than nesting arrays inside arrays, and that means they can support addressing of multiple dimensions in the [index]
syntax, where index
comes in the form of a tuple, technically.
Python does have a single dimensional array
type, that, like numpy
arrays, models homogenous C-type sequences.
Upvotes: 7