DEV Community

loading...
Cover image for Arrays vs Lists

Arrays vs Lists

harshita1786 profile image harshitaayyala ・1 min read

Arrays and Lists are almost similar but a little different.
Array is a container which can hold a fix number of items and these items should be of the same type.
List are used to store multiple items in a single variable.

  1. SIZE:
    Array - Fixed size, as resizing is expensive.
    List - It has dynamic size.

  2. INSERSIONS/DELETIONS:
    Array - Elements are usually shifted, they are inefficient.
    List - There is not shifting, hence, efficient.

  3. ACCESS:
    Array - Random access is suitable, i.e. efficient indexing.
    List - It is not suitable for operations requiring accessing elements by index such as sorting.

  4. MEMORY ALLOCATION:
    Array - No memory is wasted if the memory is full or almost full, otherwise the memory is wasted.
    List - Since memory is allocated dynamically, there is no wastage.

  5. SEQUENTIAL ACCESS:
    Array - It is faster, because the elements are in contagious memory locations.
    List - It is slower, because the elements are not in contagious memory locations.

  6. SEARCHING:
    Array - It uses binary and linear search.
    List - It uses linear search only.

  7. ORDER & SORTING:
    Array - Elements are stored consecutively.
    List - Elements are stored randomly.

Discussion (2)

pic
Editor guide
Collapse
taikedz profile image
Tai Kedzierski • Edited

This means nothing unless you specify what arrays you mean - and from the above, it seems like you are talking about the theoretical understanding of arrays and linked lists as taught in first year computer science, the usefulness of which I question.

In Python (tagged), the statements you outline are pretty much all wrong.

In Python, list is a built-in type. The Python array class from the array package is a different type which needs to be imported, and uses a list internally. Both are objects, and both are memory managed and of equal efficiency, mutable (can be changed) and sortable, indexed, etc.

Point 7 is not even true outside of Python: The conceptual definition of a List is that it is ordered. What you described is a Collection.

Data types, in fact, are very subjective from one language to another, and it's not worth spending too much time on them unless you are getting very involved in the implementation of a programming language yourself, or specifically writing C (where "array" does not exist, and "list" is whatever you want it to be).

Python is a very good language for getting things done with - but computer science basics do not apply when discussing data types.

The more fruitful part of introductory computer science will come from algorithms, state machines, formal logic, and set theory, which are applicable in many more domains and across languages. Enjoy :-)

Collapse
darkwiiplayer profile image
DarkWiiPlayer

arrays are listed. What you probably mean is a linked list, which is another implementation of lists.

As for the performance characteristics of both days structures, modern cpus with lots of caching logic make those a lot more complicated. Naive arrays will almost always perform better than naive linked lists, even in random insertions, and a clever allocator can have a huge impact on list performance as it can mean the difference between list items being mostly consecutive memory regions or spread randomly all over the ram.