4 ms·
Python lists do the job of an array probably for 99% of cases. Depends on your exact definition of an array and if that even matters
by kennydude 8y ago
Python lists do the job of an array probably for 99% of cases. Depends on your exact definition of an array and if that even matters
- umanwizard 8y agoThe subway does the job of a car in 99% of cases. Doesn't mean they're the same thing. I also wouldn't appreciate an article about subways with the title "Cars". I have literally never seen or heard of a definition of array according to which lists are arrays -- can you cite one?
- anisppp 8y agoCan you cite a common definition of “array” other than a bare minimum of being randomly accessible? (Nothing else is pinned down. Fixed vs variable length, single vs mixed type, or even underlying data structure (cf NSArray)). By the only universal definition I can think of, python lists are arrays. Python lists most certainly meet the mathematical definition of array (ie name by index)
- umanwizard 8y agoIt seems you're right. Python lists are arrays -- I didn't know that. That makes my point even stronger. The article about "Python Arrays" directly claims that Python Arrays don't exist, which is apparently wrong! The whole point is that I came away from the article believing information that is false.
- ben509 8y agoArrays are commonly an implementation, while lists are the mathematical or logical structure. Same as how a hashtable is used to implement a mapping.
- jerf 8y agoAs always, as you say, it depends on your definition. One of the useful definitions of an array is that it guarantees contiguous storage in memory. In Python, there is no way to guarantee that; even if a list is a C array of Python references under the hood, there's no way to store the information represented by those references contiguously in memory. From that perspective, pure Python doesn't have any arrays at all, which is precisely why the popular NumPy has to offer explicit support for numeric arrays. From another perspective, arrays are a set of O() guarantees for various operations. From that perspective, Python lists will generally meet the specifications that you'd write for a resizable array, or perhaps what you might call "vector", depending. CPython comes with some awful constant factors but the O() of the lists will be what you'd expect. Rather than arguing about whether Python has "arrays" interminably, it's better to carefully define array, then dispassionately examine Python lists to see if they match that definiton. You'll get a yes sometimes and no others. I say "dispassionately" because too many people seem to assign moral qualities to whether or not something conforms to a given definition, but that's not a path to understanding. No matter what definition you write down for "array", not a single characteristic of Python will change.