# Array vs List: Comparing the Two Most Popular Data Structures

By | March 26, 2022 Array and list are two of the most used data structures to store multiple values. The main difference between them (Array vs List) is that while an array is a collection of homogeneous data elements, a list is a heterogeneous collection of data elements. This means that the list can be homogeneous or heterogeneous, and thus, it can contain elements of different data types at once.

Homogeneous data elements, in case you don’t know, refer to the elements of the same data type. Heterogeneous data elements refer to values that are of different data types. Let’s continue this Array vs List discussion to know more differences between the two popular data structures.

## Array vs List: A Comprehensive Comparison

### Arrays

An array is a data structure that contains homogenous elements or elements of the same data type. Elements in an array are stored in consecutive order, i.e., at contiguous memory locations. Almost all high-level programming languages – except Python – support arrays. The following section details the syntax for defining an array in various programming languages.

#### Syntax to Define an Array in C++

```#include <conio.h>
#include <iostream.h>
void main()
{
// declare the array
int arr;
for(i=0;i<20;i++)
cin>>arr[i];
for(i=0;i<20;i++)
cout<<arr[i];
}
```

In the above code, we have declared an array arr of size 20, so the indices value of this array arr would be from 0 to 19.

#### Syntax to Define an Array Using NumPy in Python

Python does not have the concept of arrays, but you can use arrays in Python by importing them from its default numerical computation library, NumPy.

```Syntax:
from numpy import*
arr = array([1,2,3,4,5,6,7,8])
```

#### Syntax to Define an Array in Java

```class ARRAY
{
public static void main (String[] args)
{
int[] arr;
arr = new int;
arr = 10;
arr = 20;
arr = 30;
arr = 40;
arr = 50;
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i +" : "+ arr[i]);
}
}
```

### Features of Arrays

• An array is a homogeneous collection of elements.
• Total bytes = size of(Datatype) * size of array
• An array stores elements in an ordered sequence, and that’s why it is called a linear data structure.
• Arrays have the property of finiteness. This means that every array contains a finite number of elements.
• The element of an array can be accessed by its relative index number.
• An array always stores elements in a contiguous memory location.

### Operations to Perform on Arrays

• Accessing
• Inserting
• Deleting
• Traversing

• You can store multiple data elements by the same name using an array.
• An array is very fast to use.
• With the help of the index, you can access any element with the time complexity of O(1).
• Other data structures can also be implemented by using arrays, such as lists, stacks, and queues.
• Numerical calculations, like matrix operations, are easy to perform on arrays.
• You can find the address of any element of an array. For that, you need to know the address of the first element.
• 2-D arrays are used to represent matrices.

• An array is finite.
• The finiteness of the array can lead to the problem of memory wastage if you declare the size of the array more than the elements that it needs to store.
• As the array stores elements in a contiguous memory location, if you have a memory space that is enough for the array but not in a contiguous manner ,then you won’t be able to initialize the array.
• The array size cannot increase or decrease during the runtime.
• The time complexity of insertion and deletion in the array is O(n).

### Lists

Lists are similar to arrays and store elements. The only difference is that a list can store heterogeneous data elements too. You can perform many tasks on the list. However, it has some shortcomings too. But the operations performed on the list are more efficient than the arrays.

### Features of Lists

• The elements of a list are not stored in a contagious memory location.
• A Python list is a collection of data and elements.
• The elements stored in the list are generally known as Nodes.
• Each node contains a memory address of the Successor Node.
• Head Node is the first node of the list.
• If a list does not contain any value, then it is termed an Empty List.
• The last node of the list is the Tail Node.
• The tail node pointer points to NULL. It signifies the end of the list.

### Operations to Perform on Lists

• Inserting
• Deletion
• Traversing
• Sorting
• Searching for a given element
• Checking the size of the list
• Checking whether the list is empty or not
• Copying a list to another list

• Lists are dynamic data structures that are capable of adding and deleting new elements during runtime.
• The list is immune to memory wastage. In a list, you can define the size of the list according to the elements required.
• It’s easier to perform the insertion and deletion operations on the list.
• Other data structures, such as stacks and queues, can be implemented through lists.

• Compared to arrays, lists require more memory space because of the additional pointer requirement.
• Traversing the list is difficult.
• You cannot access any element of the list directly. 