If not, what are the differences
Could you please explain the difference between list and arrays?
Are they only useful when using int/floats or would you still use it for strings?
How would I go about turning a csv(just realized I wrote cvs.....) file into an array/would I even want to?
Background:
I'm learning python and data science
Videos
Both seems same to me and I can't really figure out the difference except that in array we have to use array keyword.
This is a FAQ, and as such googling something like "list Vs array python" will find many answers to this question.
As the doc says:
Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character.
So an array can hold only one numeric type that must be declared at array creation and lists may contain a mix of any object type.
In my IT programming class the teacher said lists are arrays and use them as such. But in my CS class they want us to do
from array import *
https://ccse.kennesaw.edu/fye/docs/1321/labs/m7/M7-Slides%20-%20Python.pptx
Just seems like a more complicated way to do the exact same thing as a list
The built-in array library is rarely used. There is a package called numpy that is loosely based on the same concept, but it takes it waaay farther. So anyone who is going to use an array instead of a list is going to use numpy.
Anyhow, just roll with it. It doesn't matter that much.
No. They are very different. An array can only have one kind of data in it, and it must be one of the types of data that it supports. A list can have any type of object in it or a mix of types.
A list uses a C array in the background, but that's different than the array you import from the array module.
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.
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.
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.
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.
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.
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?
Ok so I have been learning python and recently started learning about arrays. Through what I understand the main reason people use arrays and not lists in python is when they wish to do mathematical calculations(do correct me if I'm wrong). Now I heard that another reason is their flexibility. How the size of arrays can be reduced when needed and increased when needed. How is that different from list, can't you remove and add values in lists too ? Also if you can explain me other situations in which arrays are more preferrable than lists and why I would be most grateful because I am having a hard time understanding it through tutorials or web searches. Thank you
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;Hello guys, new to programming and just don't understand it.
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.
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.