3 ms·
For more efficient "lists" there are Numpy arrays https://numpy.org/doc/stable/reference/generated/numpy.array.html https://numpy.org/doc/stable/reference/gener
by killingtime74 5y ago
For more efficient "lists" there are Numpy arrays https://numpy.org/doc/stable/reference/generated/numpy.array.html https://numpy.org/doc/stable/reference/generated/numpy.array... which do have cache locality
- wodenokoto 5y agoPython does come with built-in arrays, but I've never seen them used, or heard anyone recommend them for anything. https://docs.python.org/3/library/array.html https://docs.python.org/3/library/array.html
- uranusjr 5y agoThey are recommended when you want speed, but most people writing Python probably never need that much speed (or would probably want numpy anyway), and the few who do likely have more private sources to recommend it for specific occasions. It should probably be used for more things (instead of reaching for numpy at first chance) but is still a niche tool.
- abecedarius 5y agoThe times I've used array, it was a common data structure for communicating with a C extension. You could use a Python list, but that's not just slower but more of a pain to deal with on the C side. (This was before numpy was everywhere; I don't know how much fun that might be to work with from C.) If you're going pure Python then space is a better reason than speed. I'm not sure an array is even as fast as a list.
- uranusjr 5y agoI think I use struct much more when interfacing with C, but I can see array being useful for arrays specifically when you expect more than a couple of elements. Both are still quite niche though.
- kgm 5y agoThe standard array module was more relevant in the distant past, before version 2.6 added the bytearray type. Otherwise, numpy arrays are considerably more useful in virtually every circumstance.
- edgyquant 5y agoI had never heard of them and when I try to Google Python array v set speed it just returns list v set. I did get the official documentation but it doesn’t go into speed.
- cozzyd 5y agoThese are useful when you need a pointer to an integer for a C or C++ API.