“Array” is an ambiguous term in Python and best not used. Most beginners use it to refer to the list type, but are unaware that there is actually an array.array type in the standard library as well. Answer from fiddle_n on reddit.com
🌐
Reddit
reddit.com › r/learnpython › what's the difference between array and list?
r/learnpython on Reddit: What's the difference between Array and List?
February 25, 2016 - In other words the Array is the more fundamental data type ... from Arrays you build Lists (and Dicts and Queues and Deques, and indeed most other higher-order data types). In Python an array.array is a 1-dimensional linear array; it is variable length (so it doesn't necessarily pack data together in memory), but it can only hold data of one fixed-size type, and so cannot hold other containers (for instance other array.arrays).
🌐
Reddit
reddit.com › r/python › is it ever advantageous to use a standard python list vs a numpy array when all elements are the same type?
r/Python on Reddit: Is it ever advantageous to use a standard Python list vs a numpy array when all elements are the same type?
December 8, 2020 -

I'm wondering if there is a use case where lists are better than numpy arrays besides storing multiple types of data in a relatively small/low dimension list (say i just want a quick-and-dirty list to store some constants i want to call on later?

I'm not well versed enough in how processors/memory work to know if I'm missing something else obvious. I know it won't make a huge performance impact anyways if you don't actually have a lot of math or large lists/arrays to work with, but I've been diving deeper into how numpy/pandas function at a basic level and it got me thinking about this.

🌐
Reddit
reddit.com › r/learnpython › when to use array vs. list (vs dict)
r/learnpython on Reddit: When to use array vs. list (vs dict)
November 14, 2023 -

I have lots of experience writing bad code. I realized from this question that I don't know when to use an array vs. a list. I think I'm mostly using dicts when I could be using arrays.

I do understand that arrays are more performant than lists, require an import like numpy (not built in), but what's their strategic use case? Thanks.

🌐
Reddit
reddit.com › r/learnpython › what’s the deal with arrays in python?
r/learnpython on Reddit: What’s the deal with arrays in Python?
May 28, 2024 -

I’ve recently seen some stuff out there about modules for arrays in Python but so far the only difference I can see is that the arrays have to use to same data type — what would be the advantage of that over a list?

Top answer
1 of 7
14
more efficient. python lists don't store the actual data in the list like an array of ints in Java or C would for example. they store references to objects instead, then the objects in turn have the actual data (plus some object related additional data). this a lot of overhead (but provides more flexibility). the array module removes the flexibility but increases efficiency by storing the data in the array itself akin to an int array in Java or C etc.
2 of 7
5
Like rasputin1 points out, python lists have overhead. Every piece of data in python exists as a PyObject data structure within python (defined in cpython's source code). A List in python is a vector or dynamic array of PyObjects contiguous in memory. This means that it's a fixed length array in memory which always retains some empty space as headroom for adding new items. When this headroom gets used up, everything is copied out of the old array and into a new larger one. Since PyObject itself has overhead compared to a a simple data type in C/C++, this means that a List of integers in python will take up much more memory than a C++ vector of integers of the same size would. This "of the same size" matters, because Python doesn't limit integers to any given bit width, whereas C and C++ limit integers to specific sizes. C and C++ have weird definitions of their numeric types, so here's a table showing sizes of variables on different 64 bit systems. Anyway, you should always default to using the simplest data structure that you can. Premature optimization is bad for a variety of reasons. This means default to List. Only upgrade to Array if your list is eating up way too much memory, you must keep the entire list in memory at all times, and you are not actually doing math or anything with it. If you are doing math or some complicated processing with your data, you will want to use something like Numpy, Pandas, Scipy, etc. for your processing.
Find elsewhere
Top answer
1 of 11
527

Basically, Python lists are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time. If you need to shrink and grow your list time-efficiently and without hassle, they are the way to go. But they use a lot more space than C arrays, in part because each item in the list requires the construction of an individual Python object, even for data that could be represented with simple C types (e.g. float or uint64_t).

The array.array type, on the other hand, is just a thin wrapper on C arrays. It can hold only homogeneous data (that is to say, all of the same type) and so it uses only sizeof(one object) * length bytes of memory. Mostly, you should use it when you need to expose a C array to an extension or a system call (for example, ioctl or fctnl).

array.array is also a reasonable way to represent a mutable string in Python 2.x (array('B', bytes)). However, Python 2.6+ and 3.x offer a mutable byte string as bytearray.

However, if you want to do math on a homogeneous array of numeric data, then you're much better off using NumPy, which can automatically vectorize operations on complex multi-dimensional arrays.

To make a long story short: array.array is useful when you need a homogeneous C array of data for reasons other than doing math.

2 of 11
78

For almost all cases the normal list is the right choice. The arrays module is more like a thin wrapper over C arrays, which give you kind of strongly typed containers (see docs), with access to more C-like types such as signed/unsigned short or double, which are not part of the built-in types. I'd say use the arrays module only if you really need it, in all other cases stick with lists.

🌐
Reddit
reddit.com › r/python › arrays vs list
r/Python on Reddit: arrays vs list
March 21, 2017 -

in SAS i would create something like this (using a wide data table). The code nelow creates a flag every time a diagnosis code in a row meets criteria. How would i code this in python?

array adx {} AD_DGNS PDGNS_CD DXcode1-DXcode25; array aflags {} 3 mtm_ALZHDMTA mtm_ALZH mtm_ANEMIA ; length i 3;

do i = 1 to dim(aflags);
	aflags(i) = 0;
end;

do i=1 to dim(adx);
	if(substr(adx(i),1,4)='3310')then
		do
		mtm_ALZH=1;
		end;
	if(substr(adx(i),1,5)in('33111','33119','29010','29011',
		'29012','29013','29020','29021','29040','29041','29042',
		'29043','29410','29411','29420','29421')
		or substr(adx(i),1,4)in('3310','3312','3317','2900','2903','2940'))then
		do
		mtm_ALZHDMTA=1;
		end;

	*ANEMIA(Anemia);
	if(SUBstr(adx(i),1,4)in('2800','2801','2808','2809','2810','2811','2812','2813','2814','2818',
		'2819','2820','2821','2822','2823','2825','2827','2828','2829','2830',
		'2832','2839','2842','2849','2850','2851','2853','2858','2859')
		or(SUBstr(adx(i),1,5)in('28240','28241','28242','28243','28244','28245','28246','28247','28249',
		'28260','28261','28262','28263','28264','28268','28269','28310','28311','28319'
		'28401','28409','28411','28412','28419','28481','28489','28521','28522','28529')))then
		do
		mtm_ANEMIA=1;
		end;
🌐
Reddit
reddit.com › r › Python › comments › 7aka52 › numpy_array_vs_list
Numpy array vs list : r/Python
December 29, 2016 - Stay up to date with the latest news, packages, and meta information relating to the Python programming language. --- If you have questions or are new to Python use r/LearnPython ... I can't figure out what the difference is between a Numpy array and a normal list.
🌐
Reddit
reddit.com › r/learnpython › python arrays
r/learnpython on Reddit: Python arrays
March 3, 2025 -

I'm following roadmap.sh/python to learn python. I'm at the DSA section.

I understand the concept of arrays in DSA, but what I don't understand is how to represent them in Python.

Some tutorials I've seen use lists to represent arrays (even though arrays are homogeneous and lists are heterogeneous).

I've seen some other tutorials use the arrays module to create arrays like array.array('i') where the array has only integers. But I have never seen anybody use this module to represent arrays.

I've also seen some tutorials use numpy arrays. Which I don't understand their difference from the normal arrays module.

What should I use to represent arrays?

So far I've solved a couple of arrays exercises using Python lists.

🌐
Reddit
reddit.com › r/learnprogramming › i am not understanding the point of arrays and linked lists....vs a regular list in python?
I am not understanding the point of arrays and linked lists....vs a regular list in python? : r/learnprogramming
October 8, 2021 - If there's a 'regular list' in a language it's probably a wrapper for either a linked list or an array. In Python it's stored as an array, so you can't really "use a regular list instead of an array".
🌐
LearnPython.com
learnpython.com › blog › python-array-vs-list
Array vs. List in Python – What's the Difference? | LearnPython.com
Both lists and arrays are used to store data in Python. Moreover, both data structures allow indexing, slicing, and iterating. So what's the difference between an array and a list in Python? In this article, we'll explain in detail when to use a Python array vs.
Top answer
1 of 4
4
As u/EgNotaEkkiReddit wrote, an array is simply a block of data. Usually fixed size (can't resize it without creating a new array and copying the items to it), with N elements stored sequentially in memory, numbered from 0 to N-1. A list is any sequential collection of items. You can add items to a list, remove items, sometimes even move items around. Unlike an array, a list can have different implementations: The actual data can be stored in an array, so it's easy to access any item in the middle of the list (you can ask for item #4 in the list for exapmle), but hard to add and remove items unless it's from the end of the list. This for example is known as ArrayList in Java or List in C#. The list can be a linked list - each item in the list holds a reference to the next item on the list (as in u/EgNotaEkkiReddit 's example). This makes it easy to add or remove items from the middle of the list, but difficult to access items (in order to get to item #4, you start from #0, from there to #1, to #2, etc.)
2 of 4
2
An array is simply a block of data. It starts at memory address A and ends at memory address A+X. An array is a collection of items where each item has an index, from 0 to (n-1), where n is the number of items in the array. A list is a linear sequence of items. Each item in the list points to the item after it (in the most basic implementation). You usually can only access list sequencially (begin at the first item and then go down the line) but manipulating them is easier because they aren't all stuck together in memory. Imagine an array as a group of people all in a row. You can easily walk up to them and talk to the people in the row, but you have a harder time changing their order. A list is like a lot of people randomly scattered about a room. You know who is first, so you talk to him. He can tell you who guy number 2 is. Guy number 2 can tell you who guy number three is, and so forth. It's easy to manipulate, just tell guy 4 who the new guy 5 is or change is information, but you can't just walk up to someone and say "Are you guy 9?" You have to go trough guys 1 trough 8 first.
🌐
Reddit
reddit.com › r/learnprogramming › what is the difference between a tuple, list, and array? when and why to use each one.
r/learnprogramming on Reddit: What is the difference between a Tuple, List, and Array? When and why to use each one.
August 17, 2015 -

I've read through the wikis on each and some stack overflow Q&A but km still not getting a solid difference.

To be quite honest I've only ever used arrays in my programs.

From my understanding, An array uses keys to point to memory addresses for the respective values. A list is sequential. Both an array and list are homogeneous but a tuple is heterogeneous. But thats all I've really learned about each.

Why and when would I use a tuple? I don't see a need for heterogeneous data to be gathered, it seems to me that it would just cause confusion in my code.

Any help would be greatly appreciated.

Top answer
1 of 4
3
Array: contiguous in memory (so each element is one after the other). fixed sized. very space efficient. very efficient to iterate over and access random elements. not efficient to insert something new in the middle of it, or to delete something. dynamic arrays (vectors, arraylist, etc) can be efficiently resized, and are the preferred data structure for lists unless you have a reason not to use them. (Linked) list: not continuous in memory. each node of the list contains data and a pointer/reference to the next element (and maybe a pointer to the previous). fairly efficient to iterate over. inefficient to access a random element, but efficient to access to first (and maybe last) element, efficient to add an element after (and maybe before) another, or remove an element. very efficient to merge and splice. tuple: completely different. tuples (or product types) do not represent a list of data. They are an object with a fixed dimension and each coordinate contains a particular type of data (in linked lists and arrays data is homogenous; in tuples it's often not).
2 of 4
3
Well, let's start with an array. An array is a relatively basic sort of data structure, and is generally considered to have the following characteristics: It has a fixed size It's made up of a contiguous chunk of memory As a consequence, accessing an arbitrary element inside of an array is an O(1) operation, but resizing that array or adding a new element is an O(n) operation since you need to copy every value over to a new array. A list is similar to an array, but has a variable size, and does not necessarily need to be made up of a single continuous chunk of memory. While this does make lists more useful in general then arrays, you do incur a slight performance penalty due to the overhead needed to have those nicer characteristics. I should also note that a List is not a specific kind of data structure. Rather, it's an abstract data type -- a description of what a data structure should behave like. There are several different kinds of list implementation you can try using, but the two most common ones are an array list and a linked list. An array list is basically a lightweight wrapper around an array. When the internal array is full, the array list class will automatically allocate and use a bigger array. A linked list works by chaining together a sequence of "node" objects, each of which contains a value, and points to the next and previous elements in the list. (The benefit of this data structure is that appending new values is an O(1) operation). Tuples are a little different then both arrays and lists. Arrays and lists are intended, as you said, to hold a sequence of homogenous values, whereas a tuple isn't necessarily intended to be iterated over and can hold heterogeneous values. (Tuples also are typically immutable -- you cannot change their values or add/remove new elements. That makes them more lightweight then lists). And I suppose while tuples can potentially be confusing, especially if you overdo them, they're actually really expressive and useful when you're working in a language that has first-class support for them. For example, take Python. Let's say that I want to take two distinct lists, combine each element against the next one, and iterate over them. For example: >>> names = ['Alice', 'Bob', 'Charlie', 'Danielle'] >>> ages = [8, 2, 5, 7] >>> joined = zip(names, ages) >>> print joined [('Alice', 8), ('Bob', 2), ('Charlie', 5), ('Danielle', 7)] >>> for name, age in joined: ... print(age, name) ... 8 Alice 2 Bob 5 Charlie 7 Danielle Here, I'm taking advantage of several features. I'm using the zip function to bundle together each element in both lists, producing a list of tuples. While I suppose the zip function could return a list of custom Pair objects, returning a list of tuples makes the function nicely consistent with the rest of the language. Getting key-pair values in a map returns a list of tuples, the enumerate function returns a list of tuples, etc... I can use the same syntax with all of them. I'm using tuple unpacking to automatically destructure and get every element in the tuple when I'm looping. This way, I don't have to manually pick an index to grab an element out of a tuple. I unpack it, and move on. In a nutshell, tuples free you from having to make small, custom, one-off classes whenever you want to return or work with a small bundle of data. It's a feature you have to use responsibly, but if you do, it can help make your code much less verbose.
🌐
Reddit
reddit.com › r/learnpython › differences in terms of performance between list, set, and numpy arrays?
r/learnpython on Reddit: Differences in terms of performance between list, set, and numpy arrays?
May 12, 2024 -

I already know the major differences in terms of functionality and basic performance differences, such as sets are fastest for finding an elements existence, but I would like to know a more comprehension guide to which data type is faster for certain operations / under certain conditions. For example:

  • creation

  • small vs large number of elements

  • iteration

  • modification of elements

  • getting a specific index element value

Thanks!

Top answer
1 of 4
4
Accessing a single element in a list is 5x faster than accessing a single element in a numpy array, 22.1 ns rather than 123 ns on my pc. Lists are better for single-element operations. A one-million-element multiplication operation on a numpy array is 50x faster than the similar list-comprehension operation, 1.28 ms rather than 64.2 ms. Numpy arrays are better for array operations. (These figures are copied from a test I did about 5 years ago, i.e. before the Faster CPython project, but should still be mostly relevant. The ipython %%timeit has its setup on the first line and times the second line.) In [2]: %%timeit mylist = [1,2,3] ...: x = mylist[1] ...: 22.1 ns ± 0.0626 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [3]: %%timeit import numpy; myarray = numpy.array([1,2,3]) ...: x = myarray[1] ...: 123 ns ± 0.0508 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [4]: %%timeit mylist = list(range(1000000)) ...: newlist = list(n*2 for n in mylist) ...: 64.2 ms ± 704 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [5]: %%timeit import numpy; myarray = numpy.arange(1000000) ...: newarray = myarray * 2 ...: 1.28 ms ± 1.43 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2 of 4
2
The breakeven point was arrays of size 10 or so when I tested it on some problem.  I consider 100,000x3 small for my array sizes.  It’s not even close.  Numpy is really good. That said, bad numpy code is worse than equivalent pure python code.  A for loop of a cross product implemented in python will be 2-3x faster than using numpy.  I’d still use it because I wouldn’t need to validate the formula.  That said, if you just vectorized it, it’d be 1000x faster. If you’re going to scale, you might as well start with numpy even if it’s slower right now.  As you get better, you’ll just write vectorized code and have fewer things to fix when it’s deemed too slow.
Top answer
1 of 3
10
Python array the term is "Python list" usage: everyday plain Python code NumPy array: data manipulation that needs to be fast can use Python lists if speed isn't a concern supports fast and convenient vectorized functions: write np.sqrt(array) instead of [math.sqrt(number) for number in your_list] elegantly handles arbitrary number of dimensions Pandas dataframe: for data wrangling in SQL-like language similar to in-memory SQLite database supports NumPy's vectorized functions basically a glorified NumPy array with column names
2 of 3
2
This a great question that also requires a lot of info to cover! I’ll do my best to stay on topic, but there’s so much nuance I might veer off topic a little. Let’s call “Python Arrays” Lists, since that’s mostly how the Python documentation refers to them. Lists are containers which are provided as part of the programming language. Lists are really versatile and Python provides lots of habdy builtin functions you can do with lists. NumPy arrays are indeed very similar to lists, but they were specifically designed for doing lots of number crunching in a very efficient manner. Sure, they can often be used interchangeably with lists, but if you had to calculate something like a Matrix-vector product, and you had to do it millions of times, NumPy would let you do it much faster than you ever could with Lists. Think NumPy arrays as being specialized lists. DataFrames are a bit more complex than both Lists and NumPy Arrays. I’ve seen them compared to spreadsheets quite often, and that’s a good frame of reference for getting started with DataFrames. DataFrames are tabular, like spreadsheet in Excel. Like spreadsheets, DataFrames are useful for cleaning, rearranging, and processing all sorts of data. If you’re interested in seeing DataFrames in action, I highly recommend you check out r/learnmachinelearning ! There are plenty of resources there for getting started. If you’re curious, I can go a bit more into the “why” for each, but I’d prefer to answer specific questions if anyone has any! To summarize: By default, always consider Lists first. They’re a great jack of all trades If you’re doing lots of number crunching, you might benefit for NumPy Arrays. They’re especially good when you need to work with multi-dimensional containers and access them in very specific patterns. DataFrames are more complex than either, but offer the most flexibility and structure. If you need to process something like stock prices, voting records, the CIA World Factbook, or even sometimes application logs, DataFrames can be really handy at providing functionality which you’d otherwise have to add yourself on top of Numpy Arrays or Lists.
🌐
Reddit
reddit.com › r/learnprogramming › is it bad practice to over rely on arrays / lists (js/python)
r/learnprogramming on Reddit: Is it bad practice to over rely on arrays / lists (JS/python)
September 4, 2021 -

Im a big fan of using arrays or lists in my logic, especially now that I’m learning vanilla JS. Ever since I ran into problems on a project, unable to return things from functions in my DOM event listeners, I’ve looked at arrays as my first instinct when designing logic. Something is comforting to me about being able to hang on to data, pushing and popping it around (globally:/) and all the built in methods available to manipulate them with. I’ve been doing project tutorials and I’ll go for the solution on my own first, which always is array heavy.

Is relying on arrays too much a crutch or is this just some type of programming style people develop? To be clear, when I watch the solutions, I understand how they work and I’ll write some notes on how they did it in my project, but I still find myself organizing things into arrays when I can.