Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimension array versus nested lists in python

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?

like image 812
Si Chen Avatar asked Jan 17 '26 16:01

Si Chen


1 Answers

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.

like image 187
Martijn Pieters Avatar answered Jan 20 '26 08:01

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!